90 likes | 214 Views
LL(1)-Parser. p rogram -> ’ program’ ’ ( ’ identT ’ ) ’ varlist ’ begin ’ stlist ’ end ’ v arlist -> type identlist ’ ; ’ t ype -> ’ float ’ | ’ int ’ i dentlist -> i dentT identelmlist i dentelmlist -> ’ , ’ identT identelmlist | e
E N D
LL(1)-Parser program-> ’program’ ’(’identT ’)’ varlist ’begin’ stlist ’end’ varlist-> type identlist ’;’ type-> ’float’ | ’int’ identlist-> identT identelmlist identelmlist-> ’,’identT identelmlist | e stlist-> statement stlist | e statement->identT ’=’ exp ’;’ exp->identTbexp | ’(’ exp ’)’ bexp bexp-> ’+’ exp bexp | ’-’ exp bexp | e
rpd.c #include <stdio.h> #include "rpd.h" #include "symtab.h" int nextT; void checkfor(int token) { if (token != nextT) { printf("Syntax Error \n "); exit(0); } else nextT = yylex(); }
rpd.c void program() { checkfor(programT); checkfor('('); checkfor(identT); checkfor(')'); varlist(); checkfor(beginT); stlist(); checkfor(endT); printf("Compilering faerdig. Ingen fejl\n "); }
LL(1)-Parser program-> ’program’ ’(’identT ’)’ varlist ’begin’ stlist ’end’ varlist-> type identlist ’;’ type-> ’float’ | ’int’ identlist-> identT identelmlist identelmlist-> ’,’identT identelmlist | e stlist-> statement stlist | e statement->identT ’=’ exp ’;’ exp->identTbexp | ’(’ exp ’)’ bexp bexp-> ’+’ exp bexp | ’-’ exp bexp | e
rpd.c void type() { if (nextT == floatT) { checkfor(floatT); Ctype= floatT; } else { if (nextT == intT) { checkfor(intT); Ctype= intT; } else { printf("Syntax Error 'int' eller 'float' type forvendtet! \n "); exit(0); } } }
LL(1)-Parser program-> ’program’ ’(’identT ’)’ varlist ’begin’ stlist ’end’ varlist-> type identlist ’;’ type-> ’float’ | ’int’ identlist-> identT identelmlist identelmlist-> ’,’identT identelmlist | e stlist-> statement stlist | e statement->identT ’=’ exp ’;’ exp->identTbexp | ’(’ exp ’)’ bexp bexp-> ’+’ exp bexp | ’-’ exp bexp | e
rpd.c void identelmlist() { if (nextT == ',') { checkfor(','); checkfor(identT); identelmlist(); } }
rpd.c int main() { init_sym(); nextT = yylex(); program(); }
LL(1)-Parser statement-> identT '=' expA ';' expA-> term expBexpB-> termopr term expB | etermopr-> '+' | '-'term-> factor termBtermB-> factoropr factor termB | efactoropr-> '*' | '/'factor-> uopr expA | '(' expA ')' | realT | identTuopr-> '-' | e