CSCE 531 Compiler Construction Lecture 14 Semantic Actions

  • Slides: 32
Download presentation
CSCE 531 Compiler Construction Lecture 14 Semantic Actions II Topics n Project 1 hints/revisited

CSCE 531 Compiler Construction Lecture 14 Semantic Actions II Topics n Project 1 hints/revisited n Semantics Attributes: Synthesized and Inherited Intermediate representations n n Readings: March 20, 2018

Overview Last Time n Lexical Analysis l Hash table l Token / Lexeme /

Overview Last Time n Lexical Analysis l Hash table l Token / Lexeme / Token Code n n n Review of LR Parsing Backup for an instance: LR(1) sets of items Attributes Today’s Lecture n n n Project 1 Semantic Attributes Examples References: Sections Homework: – 2– CSCE 531 Spring 2018

Lec 13 --- Last Time n LR(1) Sets of Items Construction Example n n

Lec 13 --- Last Time n LR(1) Sets of Items Construction Example n n n LR(1) Parse Table construction Useless symbols (new slides added to web) Why prefer Left recursion with LR parsing (more new slides) Today’s Lecture n Common cores – Items without lookaheads n LALR(1) Parse Table construction n Handling Ambiguous Programming Language Constructs l Precedence and associativity l Dangling-Else n n An Expression Interpreter Generating Postfix code Homework: n For the grammar in Example 4. 40 construct LR(1) sets of items, LR(1) parse table Due n Project 1 Due March 21 – 3– CSCE 531 Spring 2018

YACC Generated LALR(1) Parsers % flex lang. l // lex. yy. c % bison

YACC Generated LALR(1) Parsers % flex lang. l // lex. yy. c % bison lang. y // lang. c % gcc lex. yy. c lang. c –o parse Input source program % parse input lang. l FLEX lang. y BISON lex. yy. c yylex() lang. c yyparse() Executable Program – 4– CSCE 531 Spring 2018

Project Hints l /class/csce 531/Table & /class/csce 531/Simple. Yacc n Contrived example to illustrate

Project Hints l /class/csce 531/Table & /class/csce 531/Simple. Yacc n Contrived example to illustrate returning a pointer to symbol table and then using it as an attribute l #define YYSTYPE n struct nlist * This defines the type for yylval and all attributes on the stack (usually this is a union) l In symtab. h struct nlist { /* basic table entry */ char *name; struct nlist *next; /*next entry in chain */ int val; }; l In symtab. c n – 5– static struct nlist *hashtab[HASHSIZE]; /* pointer table */ CSCE 531 Spring 2018

Hashtable Slide 4 Lecture 4. . . double null x int. . . xbar

Hashtable Slide 4 Lecture 4. . . double null x int. . . xbar – 6– count … float … foo int. . . func boat CSCE 531 Spring 2018

In Flex/Lex Specification l %{ struct nlist *tmp; %} l %% [a-z. A-Z_]* –

In Flex/Lex Specification l %{ struct nlist *tmp; %} l %% [a-z. A-Z_]* – 7– {if((tmp=lookup(yytext)) == NULL) tmp=install(yytext); yylval = tmp; return(ID); } CSCE 531 Spring 2018

Then in the bison specification file, symt. y lexpr: expr '+' expr { strcpy(buffer,

Then in the bison specification file, symt. y lexpr: expr '+' expr { strcpy(buffer, $1 ->name); strcat(buffer, " "); strcat(buffer, $3 ->name); strcat(buffer, "+"); $$= install(strdup(buffer)); } top E 2 + E 1. . . – 8– name “xbar” link . . . Stack Attribute Stack name link “y” top E. . . Stack . . . Name link Attribute Stack CSCE 531 Spring 2018

Unions %union { struct ast *a; double d; struct symbol *s; struct symlist *sl;

Unions %union { struct ast *a; double d; struct symbol *s; struct symlist *sl; int fn; } /* which symbol */ /* which function */ Levine, John. flex & bison: Text Processing Tools (Kindle Locations 1449 -1451). O'Reilly Media. Kindle Edition. #define YYSTYPE union … // note the %union does this for us – 9– CSCE 531 Spring 2018

Beyond Syntax Slide from Authors There is a level of correctness that is deeper

Beyond Syntax Slide from Authors There is a level of correctness that is deeper than grammar fie(a, b, c, d) int a, b, c, d; {…} What is wrong with this program? (let me count the ways …) fee() { int f[3], g[0], h, i, j, k; char *p; fie(h, i, “ab”, j, k); k = f * i + j; h = g[17]; printf(“<%s, %s>. n”, p, q); p = 10; } – 10 – CSCE 531 Spring 2018

YYwrap revisited When the scanner receives an end-of-file indication from YY_INPUT, it then checks

YYwrap revisited When the scanner receives an end-of-file indication from YY_INPUT, it then checks the `yywrap()' function. n n If `yywrap()' returns false (zero), then it is assumed that the function has gone ahead and set up yyin to point to another input file, and scanning continues. If it returns true (non-zero), then the scanner terminates, returning 0 to its caller. Note that in either case, the start condition remains unchanged; it does not revert to INITIAL. If you do not supply your own version of `yywrap()', then n n – 11 – use `%option noyywrap' or link with `-lfl' to obtain the default version of the routine, which always returns 1. CSCE 531 Spring 2018

Lex Library - /usr/libl. a Libraries are typically stored away in directories On common

Lex Library - /usr/libl. a Libraries are typically stored away in directories On common place is in /usr/lib deneb> ar t /usr/libfl. a ar: /usr/libfl. a: No such file or. . . deneb> ar t /usr/libl. a allprint. o When you compile and use the –l libmain. o option it looks for a library gcc lex. yy. c –lfl –o lexer reject. o The loader takes the option ‘fl’ and builds the library name by inserting it after “/usr/lib” and then adding “. a” yyless. o yywrap. o allprint_w. o reject_w. o yyless_w. o /usr/lib. a fl – 12 – reject_e. o yyless_e. o CSCE 531 Spring 2018

The Flex Library yywrap (linux version) $ ar t /usr/libfl. a libmain. o libyywrap.

The Flex Library yywrap (linux version) $ ar t /usr/libfl. a libmain. o libyywrap. o $ ar x /usr/libfl. a libyywrap. o $ objdump -d libyywrap. o | less libyywrap. o: file format elf 32 -i 386 Disassembly of section. text: 0000 <yywrap>: 0: 55 push 1: 89 e 5 mov 3: b 8 01 00 00 00 mov 8: 5 d pop 9: c 3 ret – 13 – %ebp %esp, %ebp $0 x 1, %eax %ebp CSCE 531 Spring 2018

yywrap define your own yywrap(){ } yywrap() { // is just wrong! /* the

yywrap define your own yywrap(){ } yywrap() { // is just wrong! /* the default version in the library */ return (1); } – 14 – CSCE 531 Spring 2018

Other yyproblems yylineno n Without yacc – int yylineno=1; in definitions section in %{

Other yyproblems yylineno n Without yacc – int yylineno=1; in definitions section in %{ … %} n With Yacc - %{ extern int yylineno; %} in yacc file and above in lex should work yylval n n n Without yacc – %{ int yylval; %} in definitions section YYSTYPE – yystype stack type %union specification in Yacc l %union { » » – 15 – int ival; double dval; Symrec *tptr; } CSCE 531 Spring 2018

y. tab. h Handout deneb> more postfix. tab. h #ifndef BISON_POSTFIX_TAB_H # define BISON_POSTFIX_TAB_H

y. tab. h Handout deneb> more postfix. tab. h #ifndef BISON_POSTFIX_TAB_H # define BISON_POSTFIX_TAB_H #ifndef YYSTYPE Other Notes YYDEBUG YYTNAME #define YYMAXDEPTH 10000 pg 7/19 Yystrlen typedef union{ char *place; Yybackup int ival; } yystype; # define YYSTYPE yystype # define YYSTYPE_IS_TRIVIAL 1 #endif # define INT 257 # define ID 258 extern YYSTYPE yylval; – #endif 16 – Yydefault page 13/19 Yyreduce: … If YYDEBUG if(yydebug) … … Switch (yyn) CSCE 531 Spring 2018

Synthesized and Inherited Attributes – 17 – CSCE 531 Spring 2018

Synthesized and Inherited Attributes – 17 – CSCE 531 Spring 2018

Precedence of operators Yacc Specifications n n %prec %left %right %nonassoc Choosing between shift

Precedence of operators Yacc Specifications n n %prec %left %right %nonassoc Choosing between shift and reduce Parse: x+y*z Stack: INPUT ID E E+ E + ID +y*z y*z *z E+E – 18 – *z //Shift or reduce CSCE 531 Spring 2018

Attrbutes – 19 – CSCE 531 Spring 2018

Attrbutes – 19 – CSCE 531 Spring 2018

Postfix. y – definitions section %{ #include <ctype. h> #include <stdio. h> #define ADDOP

Postfix. y – definitions section %{ #include <ctype. h> #include <stdio. h> #define ADDOP 301 #define MULTOP 302 /* $Header: gram, v 1. 1 84/12/07 12: 01 matthews Exp $ */ extern char *yytext; char *newtemp(); %} %union{ char *place; /* symbol table pointer - attribute for ID */ int ival; /* arribute value for integer constants */ } %type <place> expr %token <ival> INT %token <place> ID %left '+' '-' – 20 – %left '*' '/' CSCE 531 Spring 2018

Postfix. y - Rules section %% task: … expr: expr '+' expr { $$

Postfix. y - Rules section %% task: … expr: expr '+' expr { $$ = newtemp(); gen(ADDOP, $1, $3, $$); } | expr '*' expr {$$ = newtemp(); gen(MULTOP, $1, $3, $$); } | ID { $<place>$ = yylval. place; } ; – 21 – CSCE 531 Spring 2018

Postfix. y - Rules section %% task: task expr 'n' | task 'n' |

Postfix. y - Rules section %% task: task expr 'n' | task 'n' | /* epsilon */ ; expr: expr '+' expr { printf("n. END n"); } { $$ = newtemp(); gen(ADDOP, $1, $3, $$); } | expr '*' expr { $$ = newtemp(); gen(MULTOP, $1, $3, $$); } | ID { $<place>$ = yylval. place; } ; – 22 – CSCE 531 Spring 2018

Postfix. y – Routines section %% char * newtemp(){ static int number = 0;

Postfix. y – Routines section %% char * newtemp(){ static int number = 0; char s[32]; char *retval, *strsave(); sprintf(s, "T%d", number); /* printf("n. NEWTEMP %s", s); */ number = number + 1; retval = strsave(s); return(retval); } gen(op, p 1, p 2, r) int op; char *p 1, *p 2, *r; – 23 – CSCE 531 Spring 2018

Gen – generate quadruples gen(int op, char *p 1, char *p 2, char *r)

Gen – generate quadruples gen(int op, char *p 1, char *p 2, char *r) { static int quadnumber = 0; quadnumber = quadnumber + 1; putchar('n'); printf("%dt", quadnumber); switch (op){ case ADDOP: printf("ADDt"); break; case MULTOP: printf("MULTt"); break; default: printf("Error in OPcode Field"); } printf("%st", p 1); printf("%st", p 2); printf("%st", r); – 24 – } CSCE 531 Spring 2018

Postfix Code Generation deneb>. /postfix z*y + q*r +foo 1 MULT z y T

Postfix Code Generation deneb>. /postfix z*y + q*r +foo 1 MULT z y T 0 2 MULT q r T 1 3 ADD T 0 T 1 T 2 4 ADD T 2 foo T 3 END – 25 – CSCE 531 Spring 2018

Tree. y – 26 – CSCE 531 Spring 2018

Tree. y – 26 – CSCE 531 Spring 2018

Tree. y – definitions section %{ #include <ctype. h> #include <stdio. h> #define YYDEBUG

Tree. y – definitions section %{ #include <ctype. h> #include <stdio. h> #define YYDEBUG 1 typedef struct TNODE { char *info; int tag; struct TNODE *left; struct TNODE *right; }TREENODE, *TREEPTR; extern char *yytext; extern TREEPTR talloc(); TREEPTR tmp; %} – 27 – CSCE 531 Spring 2018

Tree. y – definitions section continued %union { TREEPTR pval; char *cval; int ival;

Tree. y – definitions section continued %union { TREEPTR pval; char *cval; int ival; } %type <pval> expr %token <ival> INT %token <cval> ID %left '+' '-' %left '*' '/' %% – 28 – CSCE 531 Spring 2018

Tree. y – Rules section task: task expr 'n' | expr 'n' ; expr:

Tree. y – Rules section task: task expr 'n' | expr 'n' ; expr: expr '+' expr – 29 – | { printf("n. Dumping treen"); treeprint($2); printf("n"); } { $$ = talloc(); $$ -> tag = '+'; $$ -> left = $1; $$ -> right = $3; } CSCE 531 Spring 2018

expr '*' expr { $$ = talloc(); $$ -> tag = '*'; $$ ->

expr '*' expr { $$ = talloc(); $$ -> tag = '*'; $$ -> left = $1; $$ -> right = $3; } | ID { $$ = talloc(); $$ -> tag = ID; $$ -> info = yylval. cval; /* printf("reducing E -> ID: yylval. cval %s", yylval. cval); */ $$ -> left = NULL; $$ -> right = NULL; } ; %% – 30 – CSCE 531 Spring 2018

/*recursive tree print routine p 133 K&R */ treeprint(TREENODE *p) { if(p == NULL)

/*recursive tree print routine p 133 K&R */ treeprint(TREENODE *p) { if(p == NULL) return; switch(p ->tag){ case ID: printf("n ID "); printf(" %s ", p -> info); break; case '+': case '*': case '-': case '/': printf("n %c ", p -> tag); treeprint(p -> left); treeprint(p -> right); break; default: printf("error in tree: tag is %d", p -> tag); } … – 31 – CSCE 531 Spring 2018

Output of Tree deneb>. /tree x*y+z*u+f Dumping tree + + * ID x ID

Output of Tree deneb>. /tree x*y+z*u+f Dumping tree + + * ID x ID y * ID z ID u ID f – 32 – CSCE 531 Spring 2018