PLLab, NTHU,Cs2403 Programming Languages

Slides:



Advertisements
Similar presentations
Lex -- a Lexical Analyzer Generator (by M.E. Lesk and Eric. Schmidt) –Given tokens specified as regular expressions, Lex automatically generates a routine.
Advertisements

 Lex helps to specify lexical analyzers by specifying regular expression  i/p notation for lex tool is lex language and the tool itself is refered to.
176 Formal Languages and Applications: We know that Pascal programming language is defined in terms of a CFG. All the other programming languages are context-free.
Language processing: introduction to compiler construction Andy D. Pimentel Computer Systems Architecture group
Tools for building compilers Clara Benac Earle. Tools to help building a compiler C –Lexical Analyzer generators: Lex, flex, –Syntax Analyzer generator:
Chapter 3 Chang Chi-Chung. The Structure of the Generated Analyzer lexeme Automaton simulator Transition Table Actions Lex compiler Lex Program lexemeBeginforward.
COS 320 Compilers David Walker. Outline Last Week –Introduction to ML Today: –Lexical Analysis –Reading: Chapter 2 of Appel.
Lecture 2: Lexical Analysis CS 540 George Mason University.
A brief [f]lex tutorial Saumya Debray The University of Arizona Tucson, AZ
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.
Compilers: Yacc/7 1 Compiler Structures Objective – –describe yacc (actually bison) – –give simple examples of its use , Semester 1,
LEX and YACC work as a team
1 Flex. 2 Flex A Lexical Analyzer Generator  generates a scanner procedure directly, with regular expressions and user-written procedures Steps to using.
Compilers: lex/3 1 Compiler Structures Objectives – –describe lex – –give many examples of lex's use , Semester 1, Lex.
Compiler Phases: Source program Lexical analyzer Syntax analyzer Semantic analyzer Machine-independent code improvement Target code generation Machine-specific.
Using the LALR Parser Generator yacc By J. H. Wang May 10, 2011.
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.
Review: Regular expression: –How do we define it? Given an alphabet, Base case: – is a regular expression that denote { }, the set that contains the empty.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Miscellaneous 컴파일러 입문.
Scanning & FLEX CPSC 388 Ellen Walker Hiram College.
FLEX Fast Lexical Analyzer EECS Introduction Flex is a lexical analysis (scanner) generator. Flex is provided with a user input file or Standard.
CS308 Compiler Principles Introduction to Yacc Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University.
LEX (04CS1008) A tool widely used to specify lexical analyzers for a variety of languages We refer to the tool as Lex compiler, and to its input specification.
LEX AND YACC.
CPS 506 Comparative Programming Languages Syntax Specification.
Introduction to Lex Ying-Hung Jiang
Introduction to Yacc Ying-Hung Jiang
1 Using Lex. Flex – Lexical Analyzer Generator A language for specifying lexical analyzers Flex compilerlex.yy.clang.l C compiler -lfl a.outlex.yy.c a.outtokenssource.
Introduction to Lex Fan Wu
Introduction to Lexical Analysis and the Flex Tool. © Allan C. Milne Abertay University v
Flex Fast LEX analyzer CMPS 450. Lexical analysis terms + A token is a group of characters having collective meaning. + A lexeme is an actual character.
Practical 1-LEX Implementation
1 Lex & Yacc. 2 Compilation Process Lexical Analyzer Source Code Syntax Analyzer Symbol Table Intermed. Code Gen. Code Generator Machine Code.
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.
Syntax The Structure of a Language. Lexical Structure The structure of the tokens of a programming language The scanner takes a sequence of characters.
Compiler Principle and Technology Prof. Dongming LU Mar. 26th, 2014.
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.
Lex & Yacc By Hathal Alwageed & Ahmad Almadhor. References *Tom Niemann. “A Compact Guide to Lex & Yacc ”. Portland, Oregon. 18 April 2010 *Levine, John.
ICS312 LEX Set 25. LEX Lex is a program that generates lexical analyzers Converting the source code into the symbols (tokens) is the work of the C program.
Applications of Context-Free Grammars (CFG) Parsers. The YACC Parser-Generator. by: Saleh Al-shomrani.
1 LEX & YACC Tutorial February 28, 2008 Tom St. John.
Introduction to YACC CS 540 George Mason University.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Yet Another Compiler-Compiler Stephen C. Johnson July 31, 1978 YACC.
Scanner Generation Using SLK and Flex++ Followed by a Demo Copyright © 2015 Curt Hill.
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.
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,
9-December-2002cse Tools © 2002 University of Washington1 Lexical and Parser Tools CSE 413, Autumn 2002 Programming Languages
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
LEX & Yacc Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
Language processing: introduction to compiler construction
Syntax Analysis Part III
Lexical Analysis.
Tutorial On Lex & Yacc.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University
Chapter 4 Syntax Analysis.
TDDD55- Compilers and Interpreters Lesson 2
Syntax Analysis Part III
Syntax Analysis Part III
Bison Marcin Zubrowski.
Syntax Analysis Part III
Subject Name:Sysytem Software Subject Code: 10SCS52
Syntax Analysis Part III
Compiler Structures 3. Lex Objectives , Semester 2,
Compiler Lecture Note, Miscellaneous
Compiler Structures 7. Yacc Objectives , Semester 2,
Systems Programming & Operating Systems Unit – III
Presentation transcript:

PLLab, NTHU,Cs2403 Programming Languages Lex Yacc tutorial Kun-Yuan Hsieh kyshieh@pllab.cs.nthu.edu.tw Programming Language Lab., NTHU PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Overview take a glance at Lex! PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Compilation Sequence PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages What is Lex? The main job of a lexical analyzer (scanner) is to break up an input stream into more usable elements (tokens) a = b + c * d; ID ASSIGN ID PLUS ID MULT ID SEMI Lex is an utility to help you rapidly generate your scanners PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Lex – Lexical Analyzer Lexical analyzers tokenize input streams Tokens are the terminals of a language English words, punctuation marks, … Programming language Identifiers, operators, keywords, … Regular expressions define terminals/tokens PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Lex Source Program Lex source is a table of regular expressions and corresponding program fragments digit [0-9] letter [a-zA-Z] %% {letter}({letter}|{digit})* printf(“id: %s\n”, yytext); \n printf(“new line\n”); main() { yylex(); } PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Lex Source to C Program The table is translated to a C program (lex.yy.c) which reads an input stream partitioning the input into strings which match the given expressions and copying it to an output stream if necessary PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages An Overview of Lex Lex source program Lex lex.yy.c lex.yy.c C compiler a.out a.out input tokens PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Lex Source Lex source is separated into three sections by %% delimiters The general format of Lex source is The absolute minimum Lex program is thus {definitions} %% {transition rules} {user subroutines} (required) (optional) %% PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Lex v.s. Yacc Lex Lex generates C code for a lexical analyzer, or scanner Lex uses patterns that match strings in the input and converts the strings to tokens Yacc Yacc generates C code for syntax analyzer, or parser. Yacc uses grammar rules that allow it to analyze tokens from Lex and create a syntax tree. PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Lex with Yacc Lex source (Lexical Rules) Yacc source (Grammar Rules) Lex Yacc lex.yy.c y.tab.c call yylex() yyparse() Parsed Input Input return token PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Regular Expressions PLLab, NTHU,Cs2403 Programming Languages

Lex Regular Expressions (Extended Regular Expressions) A regular expression matches a set of strings Regular expression Operators Character classes Arbitrary character Optional expressions Alternation and grouping Context sensitivity Repetitions and definitions PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Operators “ \ [ ] ^ - ? . * + | ( ) $ / { } % < > If they are to be used as text characters, an escape should be used \$ = “$” \\ = “\” Every character but blank, tab (\t), newline (\n) and the list above is always a text character PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Character Classes [] [abc] matches a single character, which may be a, b, or c Every operator meaning is ignored except \ - and ^ e.g. [ab] => a or b [a-z] => a or b or c or … or z [-+0-9] => all the digits and the two signs [^a-zA-Z] => any character which is not a letter PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Arbitrary Character . To match almost character, the operator character . is the class of all characters except newline [\40-\176] matches all printable characters in the ASCII character set, from octal 40 (blank) to octal 176 (tilde~) PLLab, NTHU,Cs2403 Programming Languages

Optional & Repeated Expressions a? => zero or one instance of a a* => zero or more instances of a a+ => one or more instances of a E.g. ab?c => ac or abc [a-z]+ => all strings of lower case letters [a-zA-Z][a-zA-Z0-9]* => all alphanumeric strings with a leading alphabetic character PLLab, NTHU,Cs2403 Programming Languages

Precedence of Operators Level of precedence Kleene closure (*), ?, + concatenation alternation (|) All operators are left associative. Ex: a*b|cd* = ((a*)b)|(c(d*)) PLLab, NTHU,Cs2403 Programming Languages

Pattern Matching Primitives Metacharacter Matches . any character except newline \n newline * zero or more copies of the preceding expression + one or more copies of the preceding expression ? zero or one copy of the preceding expression ^ beginning of line / complement $ end of line a|b a or b (ab)+ one or more copies of ab (grouping) [ab] a{3} 3 instances of a “a+b” literal “a+b” (C escapes still work) PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Recall: Lex Source Lex source is a table of regular expressions and corresponding program fragments (actions) a = b + c; a operator: ASSIGNMENT b + c; … %% <regexp> <action> %% “=“ printf(“operator: ASSIGNMENT”); PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Transition Rules regexp <one or more blanks> action (C code); regexp <one or more blanks> { actions (C code) } A null statement ; will ignore the input (no actions) [ \t\n] ; Causes the three spacing characters to be ignored a = b + c; d = b * c; ↓ ↓ a=b+c;d=b*c; PLLab, NTHU,Cs2403 Programming Languages

Transition Rules (cont’d) Four special options for actions: |, ECHO;, BEGIN, and REJECT; | indicates that the action for this rule is from the action for the next rule [ \t\n] ; “ “ | “\t” | “\n” ; The unmatched token is using a default action that ECHO from the input to the output PLLab, NTHU,Cs2403 Programming Languages

Transition Rules (cont’d) REJECT Go do the next alternative … %% pink {npink++; REJECT;} ink {nink++; REJECT;} pin {npin++; REJECT;} . | \n ; PLLab, NTHU,Cs2403 Programming Languages

Lex Predefined Variables yytext -- a string containing the lexeme yyleng -- the length of the lexeme yyin -- the input stream pointer the default input of default main() is stdin yyout -- the output stream pointer the default output of default main() is stdout. cs20: %./a.out < inputfile > outfile E.g. [a-z]+ printf(“%s”, yytext); [a-z]+ ECHO; [a-zA-Z]+ {words++; chars += yyleng;} PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Lex Library Routines yylex() The default main() contains a call of yylex() yymore() return the next token yyless(n) retain the first n characters in yytext yywarp() is called whenever Lex reaches an end-of-file The default yywarp() always returns 1 PLLab, NTHU,Cs2403 Programming Languages

Review of Lex Predefined Variables Name Function char *yytext pointer to matched string int yyleng length of matched string FILE *yyin input stream pointer FILE *yyout output stream pointer int yylex(void) call to invoke lexer, returns token char* yymore(void) return the next token int yyless(int n) retain the first n characters in yytext int yywrap(void) wrapup, return 1 if done, 0 if not done ECHO write matched string REJECT go to the next alternative rule INITAL initial start condition BEGIN condition switch start condition PLLab, NTHU,Cs2403 Programming Languages

User Subroutines Section You can use your Lex routines in the same ways you use routines in other programming languages. %{ void foo(); %} letter [a-zA-Z] %% {letter}+ foo(); … void foo() { } PLLab, NTHU,Cs2403 Programming Languages

User Subroutines Section (cont’d) The section where main() is placed %{ int counter = 0; %} letter [a-zA-Z] %% {letter}+ {printf(“a word\n”); counter++;} main() { yylex(); printf(“There are total %d words\n”, counter); } PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Usage To run Lex on a source file, type lex scanner.l It produces a file named lex.yy.c which is a C program for the lexical analyzer. To compile lex.yy.c, type cc lex.yy.c –ll To run the lexical analyzer program, type ./a.out < inputfile PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Versions of Lex AT&T -- lex http://www.combo.org/lex_yacc_page/lex.html GNU -- flex http://www.gnu.org/manual/flex-2.5.4/flex.html a Win32 version of flex : http://www.monmouth.com/~wstreett/lex-yacc/lex-yacc.html or Cygwin : http://sources.redhat.com/cygwin/ Lex on different machines is not created equal. PLLab, NTHU,Cs2403 Programming Languages

Yacc - Yet Another Compiler-Compiler PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Introduction What is YACC ? Tool which will produce a parser for a given grammar. YACC (Yet Another Compiler Compiler) is a program designed to compile a LALR(1) grammar and to produce the source code of the syntactic analyzer of the language produced by this grammar. PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages How YACC Works File containing desired grammar in yacc format gram.y yacc program yacc y.tab.c C source program created by yacc cc or gcc C compiler Executable program that will parse grammar given in gram.y a.out PLLab, NTHU,Cs2403 Programming Languages

How YACC Works y.tab.h yacc y.tab.c YACC source (*.y) y.output (1) Parser generation time C compiler/linker a.out y.tab.c (2) Compile time Abstract Syntax Tree a.out Token stream (3) Run time PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages An YACC File Example PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Works with Lex How to work ? PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Works with Lex [0-9]+ call yylex() next token is NUM NUM ‘+’ NUM PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages YACC File Format %{ C declarations %} yacc declarations %% Grammar rules Additional C code Comments enclosed in /* ... */ may appear in any of the sections. PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Definitions Section %{ #include <stdio.h> #include <stdlib.h> %} %token ID NUM %start expr It is a terminal 由 expr 開始parse PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Start Symbol The first non-terminal specified in the grammar specification section. To overwrite it with %start declaraction. %start non-terminal PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Rules Section This section defines grammar Example expr : expr '+' term | term; term : term '*' factor | factor; factor : '(' expr ')' | ID | NUM; PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Rules Section Normally written like this Example: expr : expr '+' term | term ; term : term '*' factor | factor factor : '(' expr ')' | ID | NUM PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages The Position of Rules expr : expr '+' term { $$ = $1 + $3; } | term { $$ = $1; } ; term : term '*' factor { $$ = $1 * $3; } | factor { $$ = $1; } factor : '(' expr ')' { $$ = $2; } | ID | NUM PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages The Position of Rules $1 expr : expr '+' term { $$ = $1 + $3; } | term { $$ = $1; } ; term : term '*' factor { $$ = $1 * $3; } | factor { $$ = $1; } factor : '(' expr ')' { $$ = $2; } | ID | NUM PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages The Position of Rules expr : expr '+' term { $$ = $1 + $3; } | term { $$ = $1; } ; term : term '*' factor { $$ = $1 * $3; } | factor { $$ = $1; } factor : '(' expr ')' { $$ = $2; } | ID | NUM $2 PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages The Position of Rules expr : expr '+' term { $$ = $1 + $3; } | term { $$ = $1; } ; term : term '*' factor { $$ = $1 * $3; } | factor { $$ = $1; } factor : '(' expr ')' { $$ = $2; } | ID | NUM $3 Default: $$ = $1; PLLab, NTHU,Cs2403 Programming Languages

Communication between LEX and YACC [0-9]+ call yylex() next token is NUM NUM ‘+’ NUM LEX and YACC需要一套方法確認token的身份 PLLab, NTHU,Cs2403 Programming Languages

Communication between LEX and YACC Use enumeration ( 列舉 ) / define 由一方產生,另一方 include YACC 產生 y.tab.h LEX include y.tab.h yacc -d gram.y Will produce: y.tab.h PLLab, NTHU,Cs2403 Programming Languages

Communication between LEX and YACC yacc -d xxx.y Produced y.tab.h: # define CHAR 258 # define FLOAT 259 # define ID 260 # define INT 261 %{ #include <stdio.h> #include "y.tab.h" %} id [_a-zA-Z][_a-zA-Z0-9]* %% int { return INT; } char { return CHAR; } float { return FLOAT; } {id} { return ID;} scanner.l %{ #include <stdio.h> #include <stdlib.h> %} %token CHAR, FLOAT, ID, INT %% parser.y PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages YACC Rules may be recursive Rules may be ambiguous* Uses bottom up Shift/Reduce parsing Get a token Push onto stack Can it reduced (How do we know?) If yes: Reduce using a rule If no: Get another token Yacc cannot look ahead more than one token Phrase -> cart_animal AND CART | work_animal AND PLOW … PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Yacc Example Taken from Lex & Yacc Simple calculator a = 4 + 6 a a=10 b = 7 c = a + b c c = 17 $ PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Grammar expression ::= expression '+' term | expression '-' term | term term ::= term '*' factor | term '/' factor | factor factor ::= '(' expression ')' | '-' factor | NUMBER | NAME PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Parser (cont’d) statement_list: statement '\n' | statement_list statement '\n' ; statement: NAME '=' expression { $1->value = $3; } | expression { printf("= %g\n", $1); } expression: expression '+' term { $$ = $1 + $3; } | expression '-' term { $$ = $1 - $3; } | term parser.y PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Parser (cont’d) term: term '*' factor { $$ = $1 * $3; } | term '/' factor { if ($3 == 0.0) yyerror("divide by zero"); else $$ = $1 / $3; } | factor ; factor: '(' expression ')' { $$ = $2; } | '-' factor { $$ = -$2; } | NUMBER { $$ = $1; } | NAME { $$ = $1->value; } %% parser.y PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Scanner %{ #include "y.tab.h" #include "parser.h" #include <math.h> %} %% ([0-9]+|([0-9]*\.[0-9]+)([eE][-+]?[0-9]+)?) { yylval.dval = atof(yytext); return NUMBER; } [ \t] ; /* ignore white space */ scanner.l PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Scanner (cont’d) [A-Za-z][A-Za-z0-9]* { /* return symbol pointer */ yylval.symp = symlook(yytext); return NAME; } "$" { return 0; /* end of input */ } \n |”=“|”+”|”-”|”*”|”/” return yytext[0]; %% scanner.l PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages YACC Command Yacc (AT&T) yacc –d xxx.y Bison (GNU) bison –d –y xxx.y 產生y.tab.c, 與yacc相同 不然會產生xxx.tab.c PLLab, NTHU,Cs2403 Programming Languages

Precedence / Association (1) 1 – 2 - 3 (2) 1 – 2 * 3 1-2-3 = (1-2)-3? or 1-(2-3)? Define ‘-’ operator is left-association. 1-2*3 = 1-(2*3) Define “*” operator is precedent to “-” operator PLLab, NTHU,Cs2403 Programming Languages

Precedence / Association %right ‘=‘ %left '<' '>' NE LE GE %left '+' '-‘ %left '*' '/' highest precedence PLLab, NTHU,Cs2403 Programming Languages

Precedence / Association %left '+' '-' %left '*' '/' %noassoc UMINUS expr : expr ‘+’ expr { $$ = $1 + $3; } | expr ‘-’ expr { $$ = $1 - $3; } | expr ‘*’ expr { $$ = $1 * $3; } | expr ‘/’ expr { if($3==0) yyerror(“divide 0”); else $$ = $1 / $3; } | ‘-’ expr %prec UMINUS {$$ = -$2; } PLLab, NTHU,Cs2403 Programming Languages

Shift/Reduce Conflicts occurs when a grammar is written in such a way that a decision between shifting and reducing can not be made. ex: IF-ELSE ambigious. To resolve this conflict, yacc will choose to shift. PLLab, NTHU,Cs2403 Programming Languages

YACC Declaration Summary `%start' Specify the grammar's start symbol `%union' Declare the collection of data types that semantic values may have `%token' Declare a terminal symbol (token type name) with no precedence or associativity specified `%type' Declare the type of semantic values for a nonterminal symbol PLLab, NTHU,Cs2403 Programming Languages

YACC Declaration Summary `%right' Declare a terminal symbol (token type name) that is right-associative `%left' Declare a terminal symbol (token type name) that is left-associative `%nonassoc' Declare a terminal symbol (token type name) that is nonassociative (using it in a way that would be associative is a syntax error, ex: x op. y op. z is syntax error) PLLab, NTHU,Cs2403 Programming Languages

PLLab, NTHU,Cs2403 Programming Languages Reference Books lex & yacc, 2nd Edition by John R.Levine, Tony Mason & Doug Brown O’Reilly ISBN: 1-56592-000-7 Mastering Regular Expressions by Jeffrey E.F. Friedl ISBN: 1-56592-257-3 PLLab, NTHU,Cs2403 Programming Languages