170 likes | 295 Views
Computer Science at Azusa Pacific University. CS400 Compiler Construction. Sheldon X. Liang Ph. D. October 13, 2014. Azusa, CA. 1. October 13, 2014. Azusa Pacific University, Azusa, CA 91702, Tel: (800) 8 25-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/.
E N D
Computer Science at Azusa Pacific University CS400 Compiler Construction Sheldon X. Liang Ph. D. October 13, 2014 Azusa, CA 1 October 13, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction Got 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 2 October 13, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction ANTLR, Yacc, and Bison • ANTLR tool • Generates LL(k) parsers • Yacc (Yet Another Compiler Compiler) • Generates LALR(1) parsers • Bison • Improved version of Yacc 3 October 13, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction Creating an LALR(1) Parser with Yacc/Bison yaccspecificationyacc.y Yacc or Bisoncompiler y.tab.c Ccompiler y.tab.c a.out a.out inputstream outputstream 4 October 13, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction 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 } 5 October 13, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction 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%tokenTokenName 6 October 13, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction Synthesized Attributes • Semantic actions may refer to values of the synthesized attributes of terminals and nonterminals in a production:X : Y1Y2Y3 … 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 $$=$2 ( ) expr.val=x 7 October 13, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction Example 1 8 October 13, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction 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 9 October 13, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction Example 2 10 October 13, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction Example 2 (cont’d) 11 October 13, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction Combining Lex/Flex with Yacc/Bison Yacc or Bisoncompiler Yacc Specificationyacc.y y.tab.c y.tab.h Lex or Flexcompiler Lex specification lex.l and token definitionsy.tab.h lex.yy.c Ccompiler lex.yy.cy.tab.c a.out a.out outputstream inputstream 12 October 13, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction 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 Defined in y.tab.c 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 13 October 13, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction Error Recovery in Yacc %{… %}… %%lines : lines expr ‘\n’ { printf(“%g\n”, $2; } | lines ‘\n’ | /* empty */ | error ‘\n’ { yyerror(“reenter last line: ”); yyerrok; } ;… Error production:set error mode andskip input until newline Reset parser to normal mode 14 October 13, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction Organization 15 October 13, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction Got 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 16 October 13, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction Thank you very much! Questions? 17 October 13, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/