1 / 12

Lex

Lex. Linguagem (e compilador) para especificar analisadores léxicos. Organização. Programa fonte lex.l. Compilador lex ( lex ou flex). lex.yy.c. C compiler*. lex.yy.c. a.out. a.out. Entrada. Sequencia de tokens. *: “cc lex.yy.c – ll ” ou “cc lex.yy.c ”.

asher
Télécharger la présentation

Lex

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lex • Linguagem (e compilador) para especificar analisadores léxicos.

  2. Organização Programa fontelex.l Compiladorlex (lexou flex) lex.yy.c C compiler* lex.yy.c a.out a.out Entrada Sequencia de tokens *: “cc lex.yy.c –ll” ou “cc lex.yy.c”

  3. Programas Lex - estrutura Declarações – variáveis, constantes, defs.regulares %%regras de tradução – expr.regulares e ações em C Padrão { Ação } %%procedimentos auxiliares

  4. Lex - exemplo %{ /* definição de constantes LT, LE, EQ, NE,…*/ %} delim [ \t\n] ws {delim}+ letter [A-Za-z] digit [0-9] id {letter}({letter}|{digit})* number {digit}+(\.{digit}+)? (E[+\-]?{digit}+)? %% …

  5. Lex – exemplo (cont.) … {ws} {/* no action and no return */} if {return(IF);} then {return(THEN);} else {return(ELSE);} {id} {yylval=install_id();return(ID);} {number} {yylval=install_num(); return(NUMBER);} “<“ {yylval = LT; return(RELOP);} “<=“ {yylval = LE; return(RELOP);} %% …

  6. Lex – exemplo (cont.) … int install_id() { Copia lexema para a tabela de símbolos. Primeiro caracter do lexema é apontado pela variável yytext e o comprimento é definido pela variável yyleng. } Int install_num() { … }

  7. Yacc • Linguagem (e compilador) para especificar analisadores sintáticos.

  8. Organização Programa fonteparser.l Compilador yacc (yacc ou bison -d) y.tab.cy.tab.h C compiler* y.tab.clex.yy.c a.out a.out Entrada Resultadodaexecução do parser *: “cc y.tab.clex.yy.c –ly” ou “cc y.tab.clex.yy.c”

  9. Programas Yacc - estrutura Declarações – variáveis, constantes, definições%%regras de tradução – gramática e ações em C Padrão { Ação }%%procedimentos auxiliares

  10. Yacc - exemplo %{ #include <ctype.h> %} %token DIGIT %% …

  11. Yacc – exemplo (cont.) line : expr '\n' { printf("\n%d\n", $1); } expr : expr '+' term { $$ = $1 + $3; } | expr '-' expr { $$ = $1 - $3; } | term ; term : term '*' factor {$$ = $1 * $3; } | factor ; factor :'(' expr ')' { $$ = $2; } | DIGIT ; %% …

  12. Yacc – exemplo (cont.) main() { yyparse (); } yylex () { int c; c = getchar(); if (isdigit(c)) { yylval = c – ’0’; return DIGIT; } return c; }

More Related