1 October 14, 2015 1 October 14, 2015October 14, 2015October 14, 2015 Azusa, CA Sheldon X. Liang Ph....

17
1 March 25, 2022 1 March 25, 2022 March 25, 2022 Azusa, Azusa, CA CA Sheldon X. Liang Ph. D. Computer Science at Computer Science at Azusa Azusa Pacific University Pacific University Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/ CS400 Compiler Construction CS400 Compiler Construction

Transcript of 1 October 14, 2015 1 October 14, 2015October 14, 2015October 14, 2015 Azusa, CA Sheldon X. Liang Ph....

Page 1: 1 October 14, 2015 1 October 14, 2015October 14, 2015October 14, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

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

Page 2: 1 October 14, 2015 1 October 14, 2015October 14, 2015October 14, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

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

Page 3: 1 October 14, 2015 1 October 14, 2015October 14, 2015October 14, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

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

Page 4: 1 October 14, 2015 1 October 14, 2015October 14, 2015October 14, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

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

Page 5: 1 October 14, 2015 1 October 14, 2015October 14, 2015October 14, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

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

Page 6: 1 October 14, 2015 1 October 14, 2015October 14, 2015October 14, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

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

Page 7: 1 October 14, 2015 1 October 14, 2015October 14, 2015October 14, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

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

Page 8: 1 October 14, 2015 1 October 14, 2015October 14, 2015October 14, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

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

Page 9: 1 October 14, 2015 1 October 14, 2015October 14, 2015October 14, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

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

Page 10: 1 October 14, 2015 1 October 14, 2015October 14, 2015October 14, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

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

Page 11: 1 October 14, 2015 1 October 14, 2015October 14, 2015October 14, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

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

Page 12: 1 October 14, 2015 1 October 14, 2015October 14, 2015October 14, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

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

Page 13: 1 October 14, 2015 1 October 14, 2015October 14, 2015October 14, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

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

Page 14: 1 October 14, 2015 1 October 14, 2015October 14, 2015October 14, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

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

Page 15: 1 October 14, 2015 1 October 14, 2015October 14, 2015October 14, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

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

Page 16: 1 October 14, 2015 1 October 14, 2015October 14, 2015October 14, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

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

Page 17: 1 October 14, 2015 1 October 14, 2015October 14, 2015October 14, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

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