Compiler Construction Chapter 6 Compiler Construction Dr K. V. N. Sunitha
Syntax Directed Translation Syntax directed definition (SDD) is a high-level language specification for translation. Syntax directed translation (SDT) describes translation of language constructs guided by context free grammars. The grammar together with semantic action is called syntax-directed translation They are used in automatic tools like YACC. SDT helps the compiler designer to translate the language constructs directly by attaching semantic actions or subroutines. Aα + {action} = SDT Compiler Construction Dr K. V. N. Sunitha
Attributes for Grammar Symbols Synthesized attribute – The value of a synthesized attribute at a node is computed from the values of attributes at the children of that node in the parse tree. Inherited attribute – The value of an inherited attribute is computed from the values of attributes at the siblings and parent of that node. Compiler Construction Dr K. V. N. Sunitha
Writing Syntax Directed Translation Involves three steps as follows: Define grammar for input string. Take a simple string and draw parse tree. Attach semantic actions by looking at expected output. Compiler Construction Dr K. V. N. Sunitha
SDT for Evaluation of Expressions / Desk Calculator E → E + T { E•val = E•val + T•val } E → T { E•val = T•val } T → T * F { T•val = T•val * F•val } T → F { T•val = F•val } F → id { F•val = num•lval } Compiler Construction Dr K. V. N. Sunitha
Compiler Construction Annotated Parse Tree Compiler Construction Dr K. V. N. Sunitha
SDT for Converting Infix to Postfix Expression E → E + T { print(‘+’); } E → T { } T → T * F { print(‘*’); } T → F { } F → id { print(“id•name”); } Compiler Construction Dr K. V. N. Sunitha
Creation of Syntax Tree E → E + T { E•nptr = mknode ( E•nptr , ‘+’ , T•nptr ) ; } E → T { E•nptr = T•nptr; } T → T * F { T•nptr = mknode ( T•nptr , ‘*’ , F•nptr ) ; } T → F { T•nptr = F•nptr; } F → id { F•nptr = mknode ( NULL, id•lvalue , NULL ) ; } Compiler Construction Dr K. V. N. Sunitha
S-Attributed Definition It uses only synthesized attributes only. Semantic actions can be placed only at the end of right hand side of a production. Attributes are generally evaluated during bottom up parsing. Compiler Construction Dr K. V. N. Sunitha
L-Attributed Definition It allows both types. But if an inherited attribute is present there is a restriction. The restriction is – each inherited attribute is restricted to inherit either from parent or left sibling only. Translation rules can be placed anywhere on right hand side of the production. Attributes are generally evaluated by traversing the parse tree depth first and left to right. Compiler Construction Dr K. V. N. Sunitha
SDT for Storing Type Information in Symbol Table D → TL {L•type = T•type ; } T → int { T•type = int } T → char { T•type = char } L → L1 , id { L1•in = L•in ; add_type(L1•in , id) ; } L → id { add_type(L•in , id•name) ; } Compiler Construction Dr K. V. N. Sunitha
Converting L-Attributed to S-Attributed Definition D → D1 , id {D•type = D1•type ; add_type(D1•type , id•name) ; } D → T , id {L•type = T•type ; add_type(T•type , id•name) } T → int { T•type = int } T → char { T•type = char } Compiler Construction Dr K. V. N. Sunitha
Compiler Construction YACC Compiler Construction Dr K. V. N. Sunitha
Compiler Construction What Is YACC ? Automatic tool to build a parser for a given grammar. YACC (Yet Another Compiler Compiler) implements LALR(1) technique. Input is a CFG and actions to take upon recognizing a rule, i.e., SDT. Output is a parser in C. Compiler Construction Dr K. V. N. Sunitha
Compiler Construction LEX and YACC: A Team call yylex() [0-9]+ next token is NUM NUM ‘+’ NUM Compiler Construction Dr K. V. N. Sunitha
Compiler Construction Availability lex, yacc on most UNIX systems bison: a yacc replacement from GNU flex: fast lexical analyzer BSD yacc Windows/MS-DOS versions exist Compiler Construction Dr K. V. N. Sunitha
YACC Basic Operational Sequence gram.y File containing desired grammar in YACC format YACC program yacc y.tab.c C source program created by YACC cc or gcc C compiler a.out Executable program that will parse grammar given in gram.y Compiler Construction Dr K. V. N. Sunitha
YACC – A Parser Generator A language for specifying parsers and semantic analyzers lang.y Yacc compiler y.tab.c y.tab.c C compiler a.out tokens a.out syntax tree Compiler Construction Dr K. V. N. Sunitha
Compiler Construction YACC Programs %{ C declarations %} Yacc declarations %% Grammar rules Additional C code Compiler Construction Dr K. V. N. Sunitha
YACC Program for Evaluation of Expressions Input: 2+3*4 Output: 14 Compiler Construction Dr K. V. N. Sunitha
YACC Program for Evaluation of Expressions %{ #include<stdio.h> %} %token NUM %% L:L E '\n' {printf("value of expr=%d\n",$2);} |; E:E'+'T {$$=$1+$3;} | T ; T:T'*' F {$$=$1*$3;} |F F:NUM {$$=$1;} Compiler Construction Dr K. V. N. Sunitha
Compiler Construction main() { yyparse(); } void yyerror(char *s) {printf("error\n"); yylex() char c; while((c=getchar())!='\n') if(isdigit(c)) yylval=c-'0'; return NUM; else return c; } return c; Compiler Construction Dr K. V. N. Sunitha
Compiler Construction Input: 2+3*4 Output: 14 $yacc –v x.y gives y.output FSM Compiler Construction Dr K. V. N. Sunitha
Compiler Construction Lex Program %% [ \t] { } [0-9]+ {sscanf(yytext,"%d",&yylval); return num;} [\n] {return(yytext[0]);} [+*-/] {return(yytext[0]);} yywrap() { return 1; } Compiler Construction Dr K. V. N. Sunitha
Compiler Construction Parsing Parsers are already being used extensively for compiler construction, in database interfaces, in self-describing databases, in linguistics for text analysis, corpora analysis, machine translation, text analysis of b, in document preparation and conversion Compiler Construction Dr K. V. N. Sunitha