Download presentation
Presentation is loading. Please wait.
1
Lecture 14 Semantic Actions II
CSCE 531 Compiler Construction Lecture 14 Semantic Actions II Topics Project 1 hints/revisited Semantics Attributes: Synthesized and Inherited Intermediate representations Readings: March 20, 2018
2
Overview Last Time Today’s Lecture References: Sections Homework:
Lexical Analysis Hash table Token / Lexeme / Token Code Review of LR Parsing Backup for an instance: LR(1) sets of items Attributes Today’s Lecture Project 1 - Semantic Attributes Examples References: Sections Homework:
3
LR(1) Sets of Items Construction Example
Lec Last Time LR(1) Sets of Items Construction Example 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 Common cores – Items without lookaheads LALR(1) Parse Table construction Handling Ambiguous Programming Language Constructs Precedence and associativity Dangling-Else An Expression Interpreter Generating Postfix code Homework: For the grammar in Example 4.40 construct LR(1) sets of items, LR(1) parse table Due Project 1 Due March 21
4
YACC Generated LALR(1) Parsers
% flex lang.l // lex.yy.c % bison lang.y // lang.c % gcc lex.yy.c lang.c –o parse % parse input Input source program lex.yy.c yylex() lang.l FLEX lang.y BISON lang.c yyparse() Executable Program
5
Project Hints /class/csce531/Table & /class/csce531/SimpleYacc
Contrived example to illustrate returning a pointer to symbol table and then using it as an attribute #define YYSTYPE struct nlist * This defines the type for yylval and all attributes on the stack (usually this is a union) In symtab.h struct nlist { /* basic table entry */ char *name; struct nlist *next; /*next entry in chain */ int val; }; In symtab.c static struct nlist *hashtab[HASHSIZE]; /* pointer table */
6
Hashtable Slide 4 Lecture 4
. double null x int func … . xbar foo int float … . count boat
7
In Flex/Lex Specification
%{ struct nlist *tmp; %} %% [a-zA-Z_][a-zA-Z_]* {if((tmp=lookup(yytext)) == NULL) tmp=install(yytext); yylval = tmp; return(ID); }
8
Then in the bison specification file, symt.y
expr: expr '+' expr { strcpy(buffer, $1->name); strcat(buffer, " "); strcat(buffer, $3->name); strcat(buffer, "+"); $$= install(strdup(buffer)); } top E2 + E1 . . “xbar” name link top E . . “y” Name link name link Stack Attribute Stack Stack Attribute Stack
9
Unions %union { struct ast *a; double d; struct symbol *s; /* which symbol */ struct symlist *sl; int fn; /* which function */ } Levine, John. flex & bison: Text Processing Tools (Kindle Locations ). O'Reilly Media. Kindle Edition. #define YYSTYPE union … // note the %union does this for us
10
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; { … } 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; } What is wrong with this program? (let me count the ways …)
11
YYwrap revisited When the scanner receives an end-of-file indication from YY_INPUT, it then checks the `yywrap()' function. 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 use `%option noyywrap' or link with `-lfl' to obtain the default version of the routine, which always returns 1.
12
Lex Library - /usr/lib/libl.a
deneb> ar t /usr/lib/libfl.a ar: /usr/lib/libfl.a: No such file or ... deneb> ar t /usr/lib/libl.a allprint.o libmain.o reject.o yyless.o yywrap.o allprint_w.o reject_w.o yyless_w.o reject_e.o yyless_e.o Libraries are typically stored away in directories On common place is in /usr/lib When you compile and use the –l option it looks for a library gcc lex.yy.c –lfl –o lexer The loader takes the option ‘fl’ and builds the library name by inserting it after “/usr/lib/lib” and then adding “.a” /usr/lib/lib .a fl
13
The Flex Library yywrap (linux version)
$ ar t /usr/lib/libfl.a libmain.o libyywrap.o $ ar x /usr/lib/libfl.a $ objdump -d libyywrap.o | less libyywrap.o: file format elf32-i386 Disassembly of section .text: <yywrap>: 0: push %ebp 1: 89 e mov %esp,%ebp 3: b mov $0x1,%eax 8: 5d pop %ebp 9: c ret
14
yywrap define your own yywrap(){ } // is just wrong!
yywrap() { /* the default version in the library */ return (1); }
15
Other yyproblems yylineno yylval
Without yacc – int yylineno=1; in definitions section in %{ … %} With Yacc - %{ extern int yylineno; %} in yacc file and above in lex should work yylval Without yacc – %{ int yylval; %} in definitions section YYSTYPE – yystype stack type %union specification in Yacc %union { int ival; double dval; Symrec *tptr; }
16
y.tab.h Handout deneb> more postfix.tab.h
#ifndef BISON_POSTFIX_TAB_H # define BISON_POSTFIX_TAB_H #ifndef YYSTYPE typedef union{ char *place; int ival; } yystype; # define YYSTYPE yystype # define YYSTYPE_IS_TRIVIAL 1 #endif # define INT # define ID extern YYSTYPE yylval; Other Notes YYDEBUG YYTNAME #define YYMAXDEPTH pg 7/19 Yystrlen Yybackup Yydefault page 13/19 Yyreduce: … If YYDEBUG if(yydebug) … Switch (yyn)
17
Synthesized and Inherited Attributes
18
Precedence of operators
Yacc Specifications %prec %left %right %nonassoc Choosing between shift and reduce Parse: x+y*z Stack: INPUT ID +y*z E +y*z E y*z E + ID *z E + E *z //Shift or reduce
19
Attrbutes
20
Postfix.y – definitions section
%{ #include <ctype.h> #include <stdio.h> #define ADDOP 301 #define MULTOP 302 /* $Header: gram,v /12/07 12:01: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 '+' '-' %left '*' '/'
21
Postfix.y - Rules section
%% task: … expr: expr '+' expr { $$ = newtemp(); gen(ADDOP,$1, $3, $$);} | expr '*' expr {$$ = newtemp(); gen(MULTOP,$1, $3, $$); } ID { $<place>$ = yylval.place; } ;
22
Postfix.y - Rules section
%% task: task expr '\n' { printf("\nEND \n"); } | task '\n' | /* epsilon */ ; expr: expr '+' expr { $$ = newtemp(); gen(ADDOP,$1, $3, $$); | expr '*' expr { gen(MULTOP,$1, $3, $$); ID { $<place>$ = yylval.place;
23
Postfix.y – Routines section
%% char * newtemp(){ static int number = 0; char s[32]; char *retval, *strsave(); sprintf(s,"T%d",number); /* printf("\nNEWTEMP %s",s); */ number = number + 1; retval = strsave(s); return(retval); } gen(op,p1,p2,r) int op; char *p1, *p2, *r;
24
Gen – generate quadruples
gen(int op, char *p1, char *p2, char *r) { static int quadnumber = 0; quadnumber = quadnumber + 1; putchar('\n'); printf("%d\t", quadnumber); switch (op){ case ADDOP: printf("ADD\t"); break; case MULTOP: printf("MULT\t"); break; default: printf("Error in OPcode Field"); } printf("%s\t", p1); printf("%s\t", p2); printf("%s\t", r);
25
Postfix Code Generation
deneb> ./postfix z*y + q*r +foo MULT z y T0 MULT q r T1 ADD T T T2 ADD T foo T3 END
26
Tree.y
27
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; %}
28
Tree.y – definitions section continued
%union { TREEPTR pval; char *cval; int ival; } %type <pval> expr %token <ival> INT %token <cval> ID %left '+' '-' %left '*' '/' %%
29
Tree.y – Rules section task: task expr '\n' {
printf("\nDumping tree\n"); treeprint($2); printf("\n"); } | expr '\n' ; expr: expr '+' expr { $$ = talloc(); $$ -> tag = '+'; $$ -> left = $1; $$ -> right = $3; |
30
expr '*' expr { $$ = talloc(); $$ -> tag = '*'; $$ -> left = $1; $$ -> right = $3; } | ID { $$ -> tag = ID; $$ -> info = yylval.cval; /* printf("reducing E -> ID: yylval.cval %s",yylval.cval); */ $$ -> left = NULL; $$ -> right = NULL; ; %%
31
/*recursive tree print routine p133 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); default: printf("error in tree: tag is %d", p -> tag); } …
32
Output of Tree deneb> ./tree x*y+z*u+f Dumping tree + * ID x ID y
ID z ID u ID f
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.