Use of Tools for Parsing

(yacc 系ツールの原理)

10th lecture, June 10, 2016

Language Theory and Compilers

http://www.sw.it.aoyama.ac.jp/2016/Compiler/lecture10.html

Martin J. Dürst

AGU

© 2005-16 Martin J. Dürst 青山学院大学

Today's Schedule

 

Last Week's Homework

 

Summary of Last Lecture

 

How to Express Priorities

 

How to Express Associativity

 

How to Express Repetition (Lists)

 

Order of Derivation: Leftmost and Rightmost Derivation

With leftmost derivation, always the leftmost nonterminal in the syntax tree is expanded

With rightmost derivation, always the rightmost nonterminal in the syntax tree is expanded

Simple example grammar:

E → E '+' T| T
T → integer

Example of input: 5 + 7 + 3

 

Derivation Choices

Different choices may:

 

Kinds of Analysis Methods

The labels are also used for grammars:
"This grammar is LL(1)" (meaning: this grammar can be used with an LL(1) parser)

 

How to Observe and Debug bison

 

Understanding bison: The .output File

bison -v creates a file with extension .output, containing the following interesting details:

 

Understanding bison: Debuging

#define YYDEBUG 1 switches on debugging

The output shows how bison works:

 

Conflicts and Ambiguous Grammars

 

Grammar of bison Rewriting Rules

rewritingRule → nonterminalSymbol ":" rightHandList ";"
rightHandList → rightHand | rightHand "|" rightHandList
rightHand → symbolList "{" CFragment "}"
symbolList → symbol | symbol symbolList
symbol → nonterminalSimbol | terminalSymbol

 

How to Combine flex and bison

 

Advantages and Problems of Bottom-Up Parsing

 

Homework

Deadline: June 23, 2016 (Thursday in two weeks), 19:00

Prepare questions so that you can ask them in next week's lecture!

Expand the simple calculator of calc.y to a calculator for complex numbers. Immaginary numbers are expressed as 5i, complex numbers as [realPart, immaginaryPart]. Design your grammar so that inside [], real number calculations are allowed, but immaginary or complex numbers (e.g. 5i) are disallowed. Example of input: test.in

Express priorities and associativity directly in the grammar (%left, %right,... are forbidden).

Where to submit: Box in front of room O-529 (building O, 5th floor)

Submit the files complex.lex and complex.y, A4 using BOTH sides (↓↓, not ↓↑); NO cover page, staple in top left corner for more than one page, printout (non-proportional font, no wrapping lines), name (kanji and kana) and student number in comment at the top right of the first page.

 

Hints for Homework

 

Glossary

unary (operator)
単項 (演算子)
leftmost derivation
最左導出
rightmost derivation
最右導出
reverse order
逆順
lookahead
先読み
non-proportional font
等幅のフォント