Você está na página 1de 7

- Analise Lexica com Automato Finitos - Codificao da Analise Lexica (como se escreve um analisador lexico) dara token e escrever

codigo - Analise Sintatica Top-Down e Tabela de Parse - Codificacao da analise sintatica Top-Down - Analise Sintatica Botton-up e Automato LR(0) - Lex e Yacc - Traduzir codigo intermediario para assembly(8088) - Tabela de Simbolos

1- Codifique um analisador lexico que reconheca os tokens de uma expressao matematica envolvendo numeros inteiros, operacoes +, -, * / e uso de parenteses.

int numlinhas; #define NUM 256 tydef struct token{ int tag; int num; } stoken;

token * scan(void){ char look; stoken *mytoken; mytoken = (stoken *) malloc (sizeof(struct token)); int v; do{ look = getchar(); }while(look == ' '||look=='/t');

switch(look){ case '+': case '-': case '*': case '/': case '(': case ')': mytoken->tag = look; mytoken->num = -1; return mytoke; case '/n': numlinhas ++; } if(isdigit(look)){ v=0; while(isdigit(look)){ v = v*10+atoi(look);//transformar asci em inteiro look = getchar(); } mytoken->tag = NUM; mytoken->num = v; return mytoken; }else return NULL; } }

2- Considere a gramatica com as seguintes produes: E -> E+E F->(E) E->num E->E*F F->num

a- elimime a ambiguidade e a recurso esquerda A-> A1/A2/A3/1/ 2 A->1AR A-> 2AR E-> num ER ER-> +E ER ER-> *F ER ER-> F-> (E) F-> num AR->1AR| 2AR| 3AR|

b- construa a tabela de parse Num E->NumER F->Num ( ) ER-> F->(E) + ER->EER * ER->*FER $ ER->

E ER F

c- construa um analisador sintatico top-down a partir da tabela E(){ Token t; T = scan(); If(t==NULL) return false; If(t->tag == NUM) return ER(); } ER(){

Token t; T = scan(); If(T==NULL) return false; Switch(t->tag){ Case ): return false; Case +: If(E() && ER()) return true; Case *: if(F() && ER()) return true; Case $: return true; } Return false; }

F(){ Token t; int reval; T = scan(); if(t==NULL) return false; Switch(t->tag){ Case NUM: Return true; Case ( Retval = E(); T = gettoken(); If(t->tag == ) && retval) return true; } } 3- dada a gramatica com as seguintes regras de producao: 1- E-> E+T

2- E->T 3- T->(E) 4- T->id

Construa o automato LR(o) para essa gramatica e a tabela acton-goto para a analise sintatica botton-up

Solucao: criamos um novo simbolo E' -> E o estado i0 o fecho do item E'-> E

4- Mostre como fica a pilha de simbolos e estados para a cadeia id+(id) usando a tabela anterior.

5 Escreva uma especificao LEX que reconhea identificadores e as palavras reservadas if, then, e os denominadores { e } Soluo: %{ #include <stdio.h> #include <y.tab.h> Yywrap(void); %} Letter [a-zA-Z] Digit [0-9] Us [_] Id (letter|us) (letter|us|digit)* %% if {printf(if\n); return IF;} then {printf(then\n); return THEN;}

else {printf(else\n); return ELSE;}

{ {printf({\n); return {;}

} {printf(}\n); return };}

id {printf(id\n); return ID;} }} %% Main(){ yylex(); } Int yywrap(); { Return 1; } 6 Escreva uma especificao Yacc que avalie expresses aritmticas com nmeros inteiros incluindo parnteses. %{ #include <stdio.h> %} % token NUM %% Expr: expr + expr {$$ = $1 + $3;} Expr: expr + expr {$$ = $1 - $3;} Factor: NUM {$$ = yylval.num;}

Factor: ( expr ) {$$ = $2;} Expr: expr * factor {$$ = $1 * $3;} Expr: expr * factor {$$ = $1 / $3;}

Expr: fator %%

{$$ = $1;}

yacc d bas.y # create y.tab.h, y.tab.c lex bas.l # create lex.yy.c cc lex.yy.c y.tab.c obas.exe # compile/link Yacc reads the grammar descriptions in bas.y and generates a syntax analyzer (parser), that includes function yyparse, in file y.tab.c. Included in file bas.y are token declarations. The d option causes yacc to generate definitions for tokens and place them in file y.tab.h. Lex reads the pattern descriptions in bas.l, includes file y.tab.h, and generates a lexical analyzer, that includes function yylex, in file lex.yy.c. Finally, the lexer and parser are compiled and linked together to create executable bas.exe. From main we call yyparse to run the compiler. Function yyparse automatically calls yylex to obtain each token.

Você também pode gostar