1 october 14, 2015 1 october 14, 2015october 14, 2015october 14, 2015 azusa, ca sheldon x. liang ph....
Post on 31-Dec-2015
220 Views
Preview:
TRANSCRIPT
1
April 19, 20231
April 19, 2023April 19, 2023 Azusa, CAAzusa, CA
Sheldon X. Liang Ph. D.
Computer Science at Computer Science at Azusa Pacific UniversityAzusa Pacific University
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS400 Compiler ConstructionCS400 Compiler Construction
2
April 19, 20232
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
Got it with following questionsGot it with following questions• Yacc Specification
– Yacc declaration– C Declaration– User-defined procedures
• Deal with ambiguity– L/R operators’ associativity– Precedence level definition– Removing ambiguity
• Error recovery in Yacc– Set e-mode & skip input until NL– Reset parser– Diagnosis as much as possible
3
ANTLR, Yacc, and Bison
• ANTLR tool– Generates LL(k) parsers
• Yacc (Yet Another Compiler Compiler)– Generates LALR(1) parsers
• Bison– Improved version of Yacc
April 19, 20233
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
4
Creating an LALR(1) Parser with Yacc/Bison
Yacc or Bisoncompiler
yaccspecificationyacc.y
y.tab.c
inputstream
Ccompiler
a.out outputstream
y.tab.c
a.out
April 19, 20234
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
5
Yacc Specification
• A yacc specification consists of three parts:yacc declarations, and C declarations within %{ %}%% translation rules%%user-defined auxiliary procedures
• The translation rules are productions with actions:production1 { semantic action1 }production2 { semantic action2 }…productionn { semantic actionn }
April 19, 20235
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
6
Writing a Grammar in Yacc
• Productions in Yacc are of the formNonterminal : tokens/nonterminals { action }
| tokens/nonterminals { action }…;
• Tokens that are single characters can be used directly within productions, e.g. ‘+’
• Named tokens must be declared first in the declaration part using
%token TokenName
April 19, 20236
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
7
Synthesized Attributes
• Semantic actions may refer to values of the synthesized attributes of terminals and nonterminals in a production:
X : Y1 Y2 Y3 … Yn { action }– $$ refers to the value of the attribute of X– $i refers to the value of the attribute of Yi
• For examplefactor : ‘(’ expr ‘)’ { $$=$2; }
factor.val=x
expr.val=x )($$=$2
April 19, 20237
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
8
Example 1
April 19, 20238
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
9
Dealing With Ambiguous Grammars
• By defining operator precedence levels and left/right associativity of the operators, we can specify ambiguous grammars in Yacc, such asE E+E | E-E | E*E | E/E | (E) | -E | num
• To define precedence levels and associativity in Yacc’s declaration part:
%left ‘+’ ‘-’%left ‘*’ ‘/’%right UMINUS
April 19, 20239
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
10
Example 2
April 19, 202310
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
11
Example 2 (cont’d)
April 19, 202311
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
12
Combining Lex/Flex with Yacc/Bison
Yacc or Bisoncompiler
Yacc Specificationyacc.y
lex.yy.cy.tab.c
inputstream
Ccompiler
a.out outputstream
y.tab.cy.tab.h
a.out
Lex or Flexcompiler
Lex specification lex.land token definitions
y.tab.hlex.yy.c
April 19, 202312
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
13
Lex Specification for Example 2%option noyywrap%{#include “y.tab.h”
extern double yylval;%}number [0-9]+\.?|[0-9]*\.[0-9]+%%[ ] { /* skip blanks */ }{number} { sscanf(yytext, “%lf”, &yylval);
return NUMBER;}
\n|. { return yytext[0]; }
Generated by Yacc, contains#define NUMBER xxx
yacc -d example2.ylex example2.lgcc y.tab.c lex.yy.c./a.out
bison -d -y example2.yflex example2.lgcc y.tab.c lex.yy.c./a.out
Defined in y.tab.c
April 19, 202313
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
14
Error Recovery in Yacc%{…%}…%%lines : lines expr ‘\n’ { printf(“%g\n”, $2; }
| lines ‘\n’| /* empty */| error ‘\n’ { yyerror(“reenter last line: ”);
yyerrok;}
;…
Reset parser to normal modeError production:set error mode and
skip input until newlineApril 19, 2023
14Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
15
Organization
April 19, 202315
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
16
April 19, 202316
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
Got it with following questionsGot it with following questions• Yacc Specification
– Yacc declaration– C Declaration– User-defined procedures
• Deal with ambiguity– L/R operators’ associativity– Precedence level definition– Removing ambiguity
• Error recovery in Yacc– Set e-mode & skip input until NL– Reset parser– Diagnosis as much as possible
17
Thank you very much!
Questions?
April 19, 202317
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
top related