migato
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Compilador da linguagem c Compilador desenvolvido utilizando a biblioteca ply que segue as fases de analise léxica, analise sintática e analise semântica. ## Instalação Para executar o projeto basta clonar o repositório e instalar a dependencia com o código abaixo: ```sh pip3 install ply ``` Depois de terminar a instalação da biblioteca `ply` será necessário a execução do arquivo `main.py` para ver os tokens gerados e a tabela de símbolos. Esses tokens e a tabela são em relação ao código adicionado no arquivo `main.c`. ## Descrições das etapas de desenvolvimento Nessa seção será descrito brevemente como foi feito a implementação de cada etapa do compilador, sendo elas: - Análise léxica - Análise sintática - Análise semântica ### Análise léxica A análise léxica, ou tokenização, de um compilador consiste na separação de cadeias de caracteres em tokens. Para isso o analisador léxico lê todas as linhas do código de entrada e separa em partes menores de acordo com expreções regulares defnidas. Nesse compilador, existem tokens de palavras reservadas que incluem as estruturas ```if, else, for, while``` e tipos de variáveis como ```int, float```, também existem tokens de símbolos como ```=, +, -, /, >```. Além dos items citados anteriormente, a análise léxica permite atribuir valores aos tokens a partir de funções como: ```python= def t_ID(t): r'[a-zA-Z][a-zA-Z0-9]*' t.type = reserved.get(t.value, 'ID') return t ``` Nesta implementação foram ignorados símbolos de `tab`, `space` e fim de linha: ```python= t_ignore = ' \t\n' ``` ### Análise sintática A análise sintática é uma fase do processo de compilação onde é feita uma verificação da estrutura gramatical segundo uma determinada gramática formal. Para essa análise, fizemos a declaração de funções de acordo com a biblioteca `lexer`, essas funções são responsáveis pelo que o compilador irá ler e verificar se o que foi lido está dentro do esperado para a linguagem desenvolvida. Como exemplo, podemos verificar a função que faz a verificação de um while: ```python= def p_while(p): '''while : WHILE expr_condition EOL | WHILE expr_condition block''' if p[3] != ";": # block p[0] = ("WHILE", p[2], p[3]) else: # EOL p[0] = ("WHILE", p[2]) ``` Nessa função, podemos ver que o que está comentado e em letra minúscula é um símbolo não terminal que é o nome de uma outra função no código, o que faz com que o compilador vá para essa próxima função que, no exemplo acima, tem o nome de `expr_condition`. Já as palavras em letra maiúscula são os símbolos terminais, que foram declarados como palavras reservadas na parte da análise léxica, ou seja, quando o compilador faz a leitura dessa cadeira de caracteres ele não passa para uma próxima função e encerra a leitura para aquele símbolo terminal. ### Análise semântica Regras além das definidas pela análise sintática são responsabilidade da análise semântica. Utilizando a biblioteca `ply` é possível criar regras logo após a validação da análise sintática. Um exemplo é apresentado na função ```p_variable()```. Para garantir que as variáveis não declaradas sejam utilizadas no código, é validádo se esta existe na tabela de variáveis do compilador. Caso exista, é validado o seu contexto (`if` linha 10). `stash` : ```python= def p_variable(p): '''variable : ID''' ''' se não tiver na tabela adiciona na stash pra verificar a definição "depois" ''' if not p[1] in table['VARIABLE']: stash[p[1]] = {'context': context} else: if table['VARIABLE'][p[1]]['context'] < context: raise AssertionError( "On p_variable: Variable {} have context {}, but its context {}" .format(p[1], table['VARIABLE'][p[1]]['context'], str(context))) # p[0] = table['VARIABLE'][p[1]] p[0] = p[1] ``` Caso a variável ainda não exista na tabela de variáveis, ainda é possível que ela seja declarada em um momento posterior da análise semântica (visto que a análise é feita resolvendo os tokens a direita primeiro), por isso é adiciona a uma estrura `stash` para ser posteriormente validado o uso. ```python= ... if not p[1] in table['VARIABLE']: stash[p[1]] = {'context': context} ... ``` Em toda declaração de variável é checado se a variável declarada está na `stash`, de maneira que, caso uma variável seja utilizada e não declarada a `stash` manterá uma variável "solta". ```python= def p_assign(p): '''assign : ID EQUALS expr_binary EOL | type assign''' ... if p[2]["left_var"] in stash: if stash[p[2]["left_var"]]["context"] <= context: del stash[p[2]["left_var"]] else: raise AssertionError( "On p_assign definition: Stash variable {} have context {}, but its context {}" .format(p[2], stash[p[2]]['context'], str(context))) ... ``` No final da compilação é verificado se existem variáveis utilizadas e não declaradas. ```python= if len(stash) > 0: for entry in stash: print("entry in stash: {}".format(str(entry))) raise AssertionError("Stash not empty, variable used and not declared") ``` Outra regra semântica definida pelo compilador é a verificação de tipagem. Caso exista uma expressão do tipo `int a = 3`, será validada a tipagem, comparando o tipo do token `3`, com o tipo armazenado na tabela de variáveis referente a variável `a`. ```python= def p_assign(p): '''assign : ID EQUALS expr_binary EOL | type assign''' if len(p) >= 5: ''' Se a direita for um terminal tenta verificar o tipo da variavel a esquerda ''' if "TYPE_" in p[3]: right_var = {"name": "TERMINAL", "type": p[3]} if p[1] in table['VARIABLE']: ''' se for diferente do terminal sobe erro ''' if table['VARIABLE'][p[1]]['type'] != p[3]: raise AssertionError( "Variable {} is {} and should be {}". format(p[1], table['VARIABLE'][p[1]]['type'], p[3])) ``` Caso a variável ainda não existir na tabela de variáveis, o tipo da variável a direita é adicionado a uma estrutura de verificação (`verify`) que será utilizada na declaração da variável `a`. ```python= ''' se a variável a esquerda não foi declarada adiciona o tipo do terminal pra ser checado na declaração da variável a esquerda ''' if not p[1] in verify: verify[p[1]] = [] verify[p[1]].append({"name": "TERMINAL", "type": p[3]}) ``` Em toda declaração de variável é verificado se ela foi utilizada a frente no código. Caso tenha sido e com o tipo errado, o compilador dectada o erro e para a compilação. ```python= ... for key, val in verify.items(): if key == p[2]["left_var"]: for i in range(len(val)): if val[i]["type"] != "TYPE_" + str(p[1]).upper(): raise AssertionError( "Variable {} is {} and should be {}". format(key, val[i]["type"], "TYPE_" + str(p[1]).upper(), p[2])) ... ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully