Lecture 6: YACC and Syntax Directed Translation CS 540 George Mason University.

Slides:



Advertisements
Similar presentations
Chapter 2-2 A Simple One-Pass Compiler
Advertisements

Chapter 5 Syntax-Directed Translation. Translation of languages guided by context-free grammars. Attach attributes to the grammar symbols. Values of the.
Abstract Syntax Mooly Sagiv html:// 1.
Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank. Canonical LR(1) parsers detect and report the error as.
1 Beyond syntax analysis An identifier named x has been recognized. Is x a scalar, array or function? How big is x? If x is a function, how many and what.
Yacc Examples Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Design Chapter
Bottom-Up Syntax Analysis Mooly Sagiv & Greta Yorsh Textbook:Modern Compiler Design Chapter (modified)
Parser construction tools: YACC
Syntax Analysis – Part II Quick Look at Using Bison Top-Down Parsers EECS 483 – Lecture 5 University of Michigan Wednesday, September 20, 2006.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
Compilers: Yacc/7 1 Compiler Structures Objective – –describe yacc (actually bison) – –give simple examples of its use , Semester 1,
Chapter 5 Syntax-Directed Translation Section 0 Approaches to implement Syntax-Directed Translation 1、Basic idea Guided by context-free grammar (Translating.
Saumya Debray The University of Arizona Tucson, AZ 85721
LEX and YACC work as a team
Compilers: Attr. Grammars/8 1 Compiler Structures Objective – –describe semantic analysis with attribute grammars, as applied in yacc and recursive.
Automated Parser Generation (via CUP)CUP 1. High-level structure JFlexjavac Lexer spec Lexical analyzer text tokens.java CUPjavac Parser spec.javaParser.
Syntax-Directed Translation
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Using the LALR Parser Generator yacc By J. H. Wang May 10, 2011.
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Lesson 10 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 YACC Parser Generator. 2 YACC YACC (Yet Another Compiler Compiler) Produce a parser for a given grammar.  Compile a LALR(1) grammar Original written.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Miscellaneous 컴파일러 입문.
CS 363 Comparative Programming Languages Semantics.
CS308 Compiler Principles Introduction to Yacc Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University.
TDDD55- Compilers and Interpreters Lesson 1 Zeinab Ganjei Department of Computer and Information Science Linköping University.
Chapter 5: Syntax directed translation –Use the grammar to direct the translation The grammar defines the syntax of the input language. Attributes are.
–Writing a parser with YACC (Yet Another Compiler Compiler). Automatically generate a parser for a context free grammar (LALR parser) –Allows syntax direct.
Chapter 5. Syntax-Directed Translation. 2 Fig Syntax-directed definition of a simple desk calculator ProductionSemantic Rules L  E n print ( E.val.
1 Using Yacc. 2 Introduction Grammar –CFG –Recursive Rules Shift/Reduce Parsing –See Figure 3-2. –LALR(1) –What Yacc Cannot Parse It cannot deal with.
YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another.
Compiler Principles Fall Compiler Principles Lecture 6: Parsing part 5 Roman Manevich Ben-Gurion University.
1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
國立台灣大學 資訊工程學系 薛智文 98 Spring Syntax-Directed Translation (textbook ch#5.1–5.6, 4.8, 4.9 )
Introduction to YACC CS 540 George Mason University.
1 Programming Languages (CS 550) Lecture 1 Summary Grammars and Parsing Jeremy R. Johnson.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Yet Another Compiler-Compiler Stephen C. Johnson July 31, 1978 YACC.
Syntax-Directed Definitions and Attribute Evaluation Compiler Design Lecture (02/18/98) Computer Science Rensselaer Polytechnic.
LECTURE 11 Semantic Analysis and Yacc. REVIEW OF LAST LECTURE In the last lecture, we introduced the basic idea behind semantic analysis. Instead of merely.
More yacc. What is yacc – Tool to produce a parser given a grammar – YACC (Yet Another Compiler Compiler) is a program designed to compile a LALR(1) grammar.
2-1. LEX & YACC. 2 Overview  Syntax  What its program looks like –Context-free grammar, BNF  Syntax-directed translation –A grammar-oriented compiling.
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
YACC Primer CS 671 January 29, CS 671 – Spring Yacc Yet Another Compiler Compiler Automatically constructs an LALR(1) parsing table from.
YACC (Yet Another Compiler-Compiler) Chung-Ju Wu
1 Syntax Analysis Part III Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
Chapter4 Syntax-Directed Translation Introduction : 1.In the lexical analysis step, each token has its attribute , e.g., the attribute of an id is a pointer.
Syntax-Directed Translation
Compiler Construction
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Context-free Languages
Bison: Parser Generator
Syntax-Directed Translation Part I
ParserVal class public class ParserVal {   public int ival;   public double dval;   public String sval;   public Object obj;   public ParserVal(int.
CS 3304 Comparative Languages
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
Bison Marcin Zubrowski.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 4 Action Routines.
Syntax-Directed Translation Part I
Syntax-Directed Translation
Compiler Lecture Note, Miscellaneous
Compiler Structures 7. Yacc Objectives , Semester 2,
Saumya Debray The University of Arizona Tucson, AZ 85721
Syntax-Directed Translation Part I
Presentation transcript:

Lecture 6: YACC and Syntax Directed Translation CS 540 George Mason University

Part 1: Introduction to YACC

CS 540 Spring 2009 GMU3 YACC – Yet Another Compiler Compiler Lex spec YACC spec bison flexlex.yy.c y.tab.c compilera.out C/C++ tools

CS 540 Spring 2009 GMU4 YACC – Yet Another Compiler Compiler Lex spec YACC spec byacc jflexscanner.java parser.java compiler class files Java tools

CS 540 Spring 2009 GMU5 YACC Specifications Declarations % Translation rules % Supporting C/C++ code Similar structure to Lex

CS 540 Spring 2009 GMU6 YACC Declarations Section Includes: –Optional C/C++/Java code (%{ … %} ) – copied directly into y.tab.c or parser.java –YACC definitions (%token, %start, …) – used to provide additional information %token – interface to lex %start – start symbol Others: %type, %left, %right, %union …

CS 540 Spring 2009 GMU7 YACC Rules A rule captures all of the productions for a single non-terminal. –Left_side : production 1 | production 2 … | production n ; Actions may be associated with rules and are executed when the associated production is reduced.

CS 540 Spring 2009 GMU8 YACC Actions Actions are C/C++/Java code. Actions can include references to attributes associated with terminals and non-terminals in the productions. Actions may be put inside a rule – action performed when symbol is pushed on stack Safest (i.e. most predictable) place to put action is at end of rule.

CS 540 Spring 2009 GMU9 Integration with Flex (C/C++) yyparse() calls yylex() when it needs a new token. YACC handles the interface details yylval is used to return attribute information In the Lexer:In the Parser: return(TOKEN)%token TOKEN TOKEN used in productions return(‘c’)‘c’ used in productions

CS 540 Spring 2009 GMU10 Integration with Jflex (Java) In the Lexer:In the Parser: return Parser.TOKEN%token TOKEN TOKEN used in productions {return (int) yycharat(0);}‘c’ used in productions

CS 540 Spring 2009 GMU11 Building YACC parsers For input.l and input.y In input.l spec, need to #include “input.tab.h” flex input.l bison –d input.y gcc input.tab.c lex.yy.c –ly -ll the order matters

CS 540 Spring 2009 GMU12 Basic Lex/YACC example %{ #include “sample.tab.h” %} % [a-zA-Z]+ {return(NAME);} [0-9]{3}”-”[0-9]{4} {return(NUMBER); } [ \n\t]; % %token NAME NUMBER % file : file line | line ; line : NAME NUMBER ; % Lex (sample.l) YACC (sample.y)

CS 540 Spring 2009 GMU13 %token NUMBER % line : expr ; expr : expr ‘+’ term | term ; term : term ‘*’ factor | factor ; factor : ‘(‘ expr ‘)’ | NUMBER ; % Associated Lex Specification (flex)

CS 540 Spring 2009 GMU14 %{ #include “expr.tab.h” %} % \*{return(‘*’); } \+{return(‘+’); } \({return(‘(‘); } \){return(‘)’); } [0-9]+{return(NUMBER);}. ; % Associated Flex specification

CS 540 Spring 2009 GMU15 byacc Specification %{ import java.io.*; %} %token PLUS TIMES INT CR RPAREN LPAREN % lines : lines line | line ; line : expr CR ; expr : expr PLUS term | term ; term : term TIMES factor | factor ; factor: LPAREN expr RPAREN | INT ; % private scanner lexer; private int yylex() { int retVal = -1; try { retVal = lexer.yylex(); } catch (IOException e) { System.err.println("IO Error:" + e); } return retVal; } public void yyerror (String error) { System.err.println("Error : " + error + " at line " + lexer.getLine()); System.err.println("String rejected"); } public Parser (Reader r) { lexer = new scanner (r, this); } public static void main (String [] args) throws IOException { Parser yyparser = new Parser(new FileReader(args[0])); yyparser.yyparse(); }

CS 540 Spring 2009 GMU16 Associated jflex specification % %class scanner %unicode %byaccj %{ private Parser yyparser; public scanner (java.io.Reader r, Parser yyparser) { this (r); this.yyparser = yyparser; } public int getLine() { return yyline; } %} % "+" {return Parser.PLUS;} "*" {return Parser.TIMES;} "(" {return Parser.LPAREN;} ")" {return Parser.RPAREN;} [\n]{return Parser.CR;} [0-9]+ {return Parser.INT;} [ \t] {;}

CS 540 Spring 2009 GMU17 Notes: Debugging YACC conflicts: shift/reduce Sometimes you get shift/reduce errors if you run YACC on an incomplete program. Don’t stress about these too much UNTIL you are done with the grammar. If you get shift/reduce errors, YACC can generate information for you (y.output) if you tell it to (-v)

CS 540 Spring 2009 GMU18 Example: IF stmts %token IF_T THEN_T ELSE_T STMT_T % if_stmt : IF_T condition THEN_T stmt | IF_T condition THEN_T stmt ELSE_T stmt ; condition: '(' ')' ; stmt : STMT_T | if_stmt ; % This input produces a shift/reduce error

CS 540 Spring 2009 GMU19 In y.output file: 7: shift/reduce conflict (shift 10, red'n 1) on ELSE_T state 7 if_stmt : IF_T condition THEN_T stmt_ (1) if_stmt : IF_T condition THEN_T stmt_ELSE_T stmt ELSE_T shift 10. reduce 1

CS 540 Spring 2009 GMU20 Precedence/Associativity in YACC Forgetting about precedence and associativity is a major source of shift/reduce conflict in YACC. You can specify precedence and associativity in YACC, making your grammar simpler. Associativity: %left, %right, %nonassoc Precedence given order of specifications” %left PLUS MINUS %left MULT DIV %nonassoc UMINUS P in Lex/YACC book

CS 540 Spring 2009 GMU21 Precedence/Associativity in YACC %left PLUS MINUS %left MULT DIV %nonassoc UMINUS … % … expression : expression PLUS expression | expression MINUS expression …

Part 2: Syntax Directed Translation

CS 540 Spring 2009 GMU23 Syntax Directed Translation Syntax = form, Semantics = meaning Use the syntax to derive semantic information. Attribute grammar: –Context free grammar augmented by a set of rules that specify a computation –Also referred to using the more general term: Syntax Directed Definition (SDD) Evaluation of attributes grammars – can we fit with parsing?

CS 540 Spring 2009 GMU24 Attributes Associate attributes with parse tree nodes (internal and leaf). Rules (semantic actions) describe how to compute value of attributes in tree (possibly using other attributes in the tree) Two types of attributes based on how value is calculated: Synthesized & Inherited

CS 540 Spring 2009 GMU25 Example Attribute Grammar ProductionSemantic Actions E  E 1 + T E.val = E 1.val + T.val E  T E.val = T.val T  T 1 * F T.val = T 1.val * F.val T  F T.val = F.val F  num F.val = value(num) F  ( E ) F.val = E.val attributes can be associated with nodes in the parse tree E E + T FT val =

CS 540 Spring 2009 GMU26 Example Attribute Grammar ProductionSemantic Actions E  E 1 + T E.val = E 1.val + T.val E  T E.val = T.val T  T 1 * F T.val = T 1.val * F.val T  F T.val = F.val F  num F.val = value(num) F  ( E ) F.val = E.val Rule = compute the value of the attribute ‘val’ at the parent by adding together the value of the attributes at two of the children E E + T FT val =

CS 540 Spring 2009 GMU27 Synthesized Attributes Synthesized attributes – the value of a synthesized attribute for a node is computed using only information associated with the node and the node’s children (or the lexical analyzer for leaf nodes). Example: ProductionSemantic Rules A  B C D A.a := B.b + C.e A BCD

CS 540 Spring 2009 GMU28 Synthesized Attributes –Annotating the parse tree ProductionSemantic Actions E  E 1 + T E.val = E 1.val + T.val E  T E.val = T.val T  T 1 * F T.val = T 1.val * F.val T  F T.val = F.val F  num F.val = value(num) F  ( E ) F.val = E.val E E + T FT Val = A set of rules that only uses synthesized attributes is called S-attributed

CS 540 Spring 2009 GMU29 Example Problems using Synthesized Attributes Expression grammar – given a valid expression using constants (ex: 1 * 2 + 3), determine the associated value while parsing. Grid – Given a starting location of 0,0 and a sequence of north, south, east, west moves (ex: NESNNE), find the final position on a unit grid.

CS 540 Spring 2009 GMU30 Synthesized Attributes – Expression Grammar ProductionSemantic Actions E  E 1 + T E.val = E 1.val + T.val E  T E.val = T.val T  T 1 * F T.val = T 1.val * F.val T  F T.val = F.val F  num F.val = value(num) F  ( E ) F.val = E.val

CS 540 Spring 2009 GMU31 Synthesized Attributes –Annotating the parse tree ProductionSemantic Actions E  E 1 + T E.val = E 1.val + T.val E  T E.val = T.val T  T 1 * F T.val = T 1.val * F.val T  F T.val = F.val F  num F.val = value(num) F  ( E ) F.val = E.val E E + T F Num = 4 T T * F Num = 3 Num = 2 F Val = Input: 2 * Val =

CS 540 Spring 2009 GMU32 Synthesized Attributes –Annotating the parse tree ProductionSemantic Actions E  E 1 + T E.val = E 1.val + T.val E  T E.val = T.val T  T 1 * F T.val = T 1.val * F.val T  F T.val = F.val F  num F.val = value(num) F  ( E ) F.val = E.val E E + T F Num = 4 T T * F Num = 3 Num = 2 F Val = 2 Val = 3 Val = 4 Val = Input: 2 * Val =

CS 540 Spring 2009 GMU33 Synthesized Attributes –Annotating the parse tree ProductionSemantic Actions E  E 1 + T E.val = E 1.val + T.val E  T E.val = T.val T  T 1 * F T.val = T 1.val * F.val T  F T.val = F.val F  num F.val = value(num) F  ( E ) F.val = E.val E E + T F Num = 4 T T * F Num = 3 Num = 2 F Val = 2 Val = 3 Val = 4 Val =2 Val = Input: 2 * Val = Val = 4

CS 540 Spring 2009 GMU34 Synthesized Attributes –Annotating the parse tree ProductionSemantic Actions E  E 1 + T E.val = E 1.val + T.val E  T E.val = T.val T  T 1 * F T.val = T 1.val * F.val T  F T.val = F.val F  num F.val = value(num) F  ( E ) F.val = E.val E E + T F Num = 4 T T * F Num = 3 Num = 2 F Val = 2 Val = 3 Val =4 Val =2 Val =6 Input: 2 * Val =6 Val =10 Val =4

CS 540 Spring 2009 GMU35 Synthesized Attributes –Annotating the parse tree ProductionSemantic Actions E  E 1 + T E.val = E 1.val + T.val E  T E.val = T.val T  T 1 * F T.val = T 1.val * F.val T  F T.val = F.val F  num F.val = value(num) F  ( E ) F.val = E.val E E + T T * F Num = 4 T Num = 3 Num = 2 F Val = Input: * 3 F Val =

CS 540 Spring 2009 GMU36 Synthesized Attributes –Annotating the parse tree ProductionSemantic Actions E  E 1 + T E.val = E 1.val + T.val E  T E.val = T.val T  T 1 * F T.val = T 1.val * F.val T  F T.val = F.val F  num F.val = value(num) F  ( E ) F.val = E.val E E + T T * F Num = 4 T Num = 3 Num = 2 F Val =2 Val =12 Val =2 Val =14 Input: * 3 F Val =4 Val =3

CS 540 Spring 2009 GMU37 Grid Example Given a starting location of 0,0 and a sequence of north, south, east, west moves (ex: NEENNW), find the final position on a unit grid. start final

CS 540 Spring 2009 GMU38 Synthesized Attributes – Grid Positions ProductionSemantic Actions seq  seq 1 instr seq.x = seq 1.x + instr.dx seq.y = seq 1.y + instr.dy seq  BEGIN seq.x = 0, seq.y = 0 instr  NORTH instr.dx = 0, instr.dy = 1 instr  SOUTH instr.dx = 0, instr.dy = -1 instr  EAST instr.dx = 1, instr.dy = 0 instr  WEST instr.dx = -1, instr.dy = 0

CS 540 Spring 2009 GMU39 Synthesized Attributes –Annotating the parse tree ProductionSemantic Actions seq  seq 1 instr seq.x = seq 1.x + instr.dx seq.y = seq 1.y + instr.dy seq  BEGIN seq.x = 0, seq.y = 0 instr  NORTH instr.dx = 0, instr.dy = 1 instr  SOUTH instr.dx = 0, instr.dy = -1 instr  EAST instr.dx = 1, instr.dy = 0 instr  WEST instr.dx = -1, instr.dy = 0 seq seq instr BEGINN W S S Input: BEGIN N W S S x= y= x= y= x= y= x= y= x= y= dx=0 dy=1 dx=-1 dy=0 dx=0 dy=-1 dx=0 dy=-1

CS 540 Spring 2009 GMU40 Synthesized Attributes –Annotating the parse tree ProductionSemantic Actions seq  seq 1 instr seq.x = seq 1.x + instr.dx seq.y = seq 1.y + instr.dy seq  BEGIN seq.x = 0, seq.y = 0 instr  NORTH instr.dx = 0, instr.dy = 1 instr  SOUTH instr.dx = 0, instr.dy = -1 instr  EAST instr.dx = 1, instr.dy = 0 instr  WEST instr.dx = -1, instr.dy = 0 seq seq instr BEGINN W S S Input: BEGIN N W S S x=0 y=0 x=0 y=1 x=-1 y=1 x=-1 y=0 x=-1 y=-1 dx=0 dy=1 dx=-1 dy=0 dx=0 dy=-1 dx=0 dy=-1

CS 540 Spring 2009 GMU41 Inherited Attributes Inherited attributes – if an attribute is not synthesized, it is inherited. Example: ProductionSemantic Rules A  B C D B.b := A.a + C.b A BCD

CS 540 Spring 2009 GMU42 Inherited Attributes – Determining types ProductionsSemantic Actions Decl  Type List List.in = Type.type Type  int Type.type = INT Type  real T.type = REAL List  List 1, id List 1.in = List.in, addtype(id.entry.List.in) List  id addtype(id.entry,List.in)

CS 540 Spring 2009 GMU43 Inherited Attributes – Example ProductionsSemantic Actions Decl  Type List List.in = Type.type Type  int Type.type = INT Type  real T.type = REAL List  List 1, id List 1.in = List.in, addtype(id.entry.List.in) List  id addtype(id.entry,List.in) Decl Type List List, id id int = a = b = c type=INT in= Input: int a,b,c

CS 540 Spring 2009 GMU44 Inherited Attributes – Example ProductionsSemantic Actions Decl  Type List List.in = Type.type Type  int Type.type = INT Type  real T.type = REAL List  List 1, id List 1.in = List.in, addtype(id.entry.List.in) List  id addtype(id.entry,List.in) Decl Type List List, id id int = a = b = c type=INT in=INT Input: int a,b,c

CS 540 Spring 2009 GMU45 Attribute Dependency An attribute b depends on an attribute c if a valid value of c must be available in order to find the value of b. The relationship among attributes defines a dependency graph for attribute evaluation. Dependencies matter when considering syntax directed translation in the context of a parsing technique.

CS 540 Spring 2009 GMU46 Attribute Dependencies ProductionSemantic Actions E  E 1 + T E.val = E 1.val + T.val E  T E.val = T.val T  T 1 * F T.val = T 1.val * F.val T  F T.val = F.val F  num F.val = value(num) F  ( E ) F.val = E.val E E + T T * F Num = 4 T Num = 3 Num = 2 F Val = 2 Val = 12 Val = 2 Val = 14 F Val = 4 Val = 3 Synthesized attributes – dependencies always up the tree

CS 540 Spring 2009 GMU47 Attribute Dependencies ProductionsSemantic Actions Decl  Type List List.in = Type.type Type  int Type.type = INT Type  real T.type = REAL List  List 1, id List 1.in = List.in, addtype(id.entry.List.in) List  id addtype(id.entry,List.in) Decl Type List List, id id int = a = b = c Type=int in=int addtype(c,int) in=int addtype(b,int) in=int addtype(a,int)

CS 540 Spring 2009 GMU48 Attribute Dependencies ProductionsSemantic Actions A  B A.s = B.i B.i = A.s + 1 A B s= i= Circular dependences are a problem

CS 540 Spring 2009 GMU49 Synthesized Attributes and LR Parsing Synthesized attributes have natural fit with LR parsing Attribute values can be stored on stack with their associated symbol When reducing by production A  , both  and the value of  ’s attributes will be on the top of the LR parse stack!

CS 540 Spring 2009 GMU50 Synthesized Attributes and LR Parsing Example Stack: $0[attr],a1[attr],T2[attr],b5[attr],c8[attr] Stack after T  T b c: $0[attr],a1[attr],T2[attr’] a b T b c a b T b c T

CS 540 Spring 2009 GMU51 Other SDD types L-Attributed definition – edges can go from left to right, but not right to left. Every attribute must be: Synthesized or Inherited (but limited to ensure the left to right property).

Part 3: Back to YACC

CS 540 Spring 2009 GMU53 Attributes in YACC You can associate attributes with symbols (terminals and non-terminals) on right side of productions. Elements of a production referred to using ‘$’ notation. Left side is $$. Right side elements are numbered sequentially starting at $1. For A : B C D, A is $$, B is $1, C is $2, D is $3. Default attribute type is int. Default action is $$ = $1;

CS 540 Spring 2009 GMU54 Back to Expression Grammar ProductionSemantic Actions E  E 1 + T E.val = E 1.val + T.val E  T E.val = T.val T  T 1 * F T.val = T 1.val * F.val T  F T.val = F.val F  num F.val = value(num) F  ( E ) F.val = E.val E E + T F Num = 4 T T * F Num = 3 Num = 2 F Val = 2 Val = 3 Val = 4 Val = 2 Val = 6 Val = 4 Val = 6 Val = 10 Input: 2 * 3 + 4

CS 540 Spring 2009 GMU55 Expression Grammar in YACC %token NUMBER CR % lines: lines line | line ; line: expr CR{printf(“Value = %d”,$1); } ; expr: expr ‘+’ term{ $$ = $1 + $3; } | term{ $$ = $1; /* default – can omit */} ; term: term ‘*’ factor{ $$ = $1 * $3; } | factor ; factor: ‘(‘ expr ‘)’{ $$ = $2; } | NUMBER ; %

CS 540 Spring 2009 GMU56 Expression Grammar in YACC %token NUMBER CR % lines: lines line | line ; line: expr CR{System.out.println($1.ival); } ; expr: expr ‘+’ term{$$ = new ParserVal($1.ival + $3.ival); } | term ; term: term ‘*’ factor {$$ = new ParserVal($1.ival * $3.ival); | factor ; factor: ‘(‘ expr ‘)’{$$ = new ParserVal($2.ival); } | NUMBER ; %

CS 540 Spring 2009 GMU57 Associated Lex Specification % \+{return(‘+’); } \*{return(‘*’); } \({return(‘(’); } \){return(‘)’); } [0-9]+{yylval = atoi(yytext); return(NUMBER); } [\n]{return(CR);} [ \t]; % In Java: yyparser.yylval = new ParserVal(Integer.parseInt(yytext())); return Parser.INT;

CS 540 Spring 2009 GMU58 A : B {action1} C {action2} D {action3}; Actions can be embedded in productions. This changes the numbering ($1,$2,…) Embedding actions in productions not always guaranteed to work. However, productions can always be rewritten to change embedded actions into end actions. A : new_B new_C D {action3}; new_b : B {action1}; new_C : C {action 2} ; Embedded actions are executed when all symbols to the left are on the stack.

CS 540 Spring 2009 GMU59 Non-integer Attributes in YACC yylval assumed to be integer if you take no other action. First, types defined in YACC definitions section. %union{ type1 name1; type2 name2; … }

CS 540 Spring 2009 GMU60 Next, define what tokens and non-terminals will have these types: %token token %type non-terminal In the YACC spec, the $n symbol will have the type of the given token/non-terminal. If type is a record, field names must be used (i.e. $n.field). In Lex spec, use yylval.name in the assignment for a token with attribute information. Careful, default action ($$ = $1;) can cause type errors to arise.

CS 540 Spring 2009 GMU61 Example 2 with floating pt. %union{ double f_value; } %token NUMBER %type expr term factor % expr : expr ‘+’ term { $$ = $1 + $3; } | term ; term : term ‘*’ factor { $$ = $1 * $3; } | factor ; factor : ‘(‘ expr ‘)’ { $$ = $2; } | NUMBER ; % #include “lex.yy.c”

CS 540 Spring 2009 GMU62 Associated Lex Specification % \*{return(‘*’); } \+{return(‘+’); } \({return(‘(‘); } \){return(‘)’); } [0-9]*“.”[0-9]+ {yylval.f_value = atof(yytext); return(NUMBER);} %

CS 540 Spring 2009 GMU63 When type is a record: Field names must be used -- $n.field has the type of the given field. In Lex, yylval uses the complete name: yylval.typename.fieldname If type is pointer to a record,  is used (as in C/C++).

CS 540 Spring 2009 GMU64 Example with records ProductionSemantic Actions seq  seq 1 instr seq.x = seq 1.x + instr.dx seq.y = seq 1.y + instr.dy seq  BEGIN seq.x = 0, seq.y = 0 instr  N instr.dx = 0, instr.dy = 1 instr  S instr.dx = 0, instr.dy = -1 instr  E instr.dx = 1, instr.dy = 0 instr  W instr.dx = -1, instr.dy = 0

CS 540 Spring 2009 GMU65 Example in YACC %union{ struct s1 {int x; int y} pos; struct s2 {int dx; int dy} offset; } %type seq %type instr % seq : seq instr {$$.x = $1.x+$2.dx; $$.y = $1.y+$2.dy; } | BEGIN {$$.x=0; $$.y = 0; }; instr : N {$$.dx = 0; $$.dy = 1;} | S {$$.dx = 0; $$.dy = -1;} … ;

CS 540 Spring 2009 GMU66 Attribute oriented YACC error messages %union{ struct s1 {int x; int y} pos; struct s2 {int dx; int dy} offset; } %type seq %type instr % seq : seq instr {$$.x = $1.x+$2.dx; $$.y = $1.y+$2.dy; } | BEGIN {$$.x=0; $$.y = 0; }; instr : N | S {$$.dx = 0; $$.dy = -1;} … ; yacc example2.y "example2.y", line 13: fatal: default action causes potential type clash missing action

CS 540 Spring 2009 GMU67 Java’s ParserVal class public class ParserVal { public int ival; public double dval; public String sval; public Object obj; public ParserVal(int val) { ival=val; } public ParserVal(double val) { dval=val; } public ParserVal(String val) { sval=val; } public ParserVal(Object val) { obj=val; } }

CS 540 Spring 2009 GMU68 If ParserVal won’t work… Can define and use your own Semantic classes: /home/u1/white/byacc -Jsemantic=Semantic gen.y

CS 540 Spring 2009 GMU69 Grid Example (Java) % grid : seq {System.out.println("Done: " + $1.ival1 + " " + $1.ival2);} ; seq : seq instr {$$.ival1 = $1.ival1 + $2.ival1; $$.ival2 = $1.ival2 + $2.ival2;} | BEGIN ; instr : N | S | E | W ; % public static final class Semantic { public int ival1 ; public int ival2 ; public Semantic(Semantic sem) { ival1 = sem.ival1; ival2 = sem.ival2; } public Semantic(int i1,int i2) { ival1 = i1; ival2 = i2; } public Semantic() { ival1=0;ival2=0;} } /home/u1/white/byacc -Jsemantic=Semantic gen.y

CS 540 Spring 2009 GMU70 Grid Example (Java) % B {yyparser.yylval = new Parser.Semantic(0,0); return Parser.BEGIN;} N {yyparser.yylval = new Parser.Semantic(0,1); return Parser.N;} S {yyparser.yylval = new Parser.Semantic(0,-1); return Parser.S;} E {yyparser.yylval = new Parser.Semantic(1,0); return Parser.E;} W {yyparser.yylval = new Parser.Semantic(-1,0); return Parser.W;} [ \t\n] {;} %