Use of Tools for Parsing

(構文解析用のツールの詳細)

10th lecture, June 21, 2019

Language Theory and Compilers

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

Martin J. Dürst

AGU

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

Today's Schedule

 

Last Lecture's Homework

都合により削除

 

Summary of Last Lecture

 

Leftovers from Last Lecture

 

How to Express Priorities

 

Grammar Patterns: Priority

(priority is small_exp > middle_exp > big_exp; assuming left associative)

big_exp: big_exp operator middle_exp
| middle_exp
;
middle_exp: middle_exp operator small_exp
| small_exp
;

 

How to Express Associativity

 

Grammar Patterns: Associativity

Left associative:

big_exp: big_exp left_assoc_op small_exp
| small_exp
;

Right associative:

big_exp: small_exp right_assoc_op big_exp
| small_exp
;

 

How to Express Repetition (Lists)

 

Grammar Patterns: Repetition

Zero or more times:

items: items item
|
;

One or more times:

items: items item
| item
;

Instead of "items item", "item items" is also possible, but bison's stack may become a problem

 

Grammar Patterns: Parentheses

small_exp: open_paren big_exp close_paren
| literal
;

 

How to Express Other Constructs (e.g. if statement)

Write as is, carefully distinguishing alternatives and terminal/non-terminal symbols

if_statement : IF OPENPAREN cond CLOSEPAREN statement
             | IF OPENPAREN cond CLOSEPAREN statement
               ELSE statement
;

 

Order of Derivation: Leftmost and Rightmost Derivation

With leftmost derivations, the leftmost nonterminal in the syntax tree is always expanded first

With rightmost derivations, the rightmost nonterminal in the syntax tree is always expanded first

Simple example grammar:

E → E '-' T | T
T → integer

Example of input: 5 - 7 - 3

 

Derivation Choices

Different choices may generate:

 

Kinds of Analysis Methods

The labels are also used for grammars:
grammar g is LL(1) ⇔ grammar g can be used with an LL(1) parser.

 

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

 

Another Example of Ambiguity

The grammar for if-else is a famous example of ambiguity:

if (...) if (...) ...; else ...;

can be parsed in two ways:

if (...) {
    if (...)  ...;
    else      ...;
}

or

if (...) {
    if (...)  ...;
}
else  ...;

This creates a shift-reduce conflict.

The first way of parsing is correct (for C), and is choosen by bison because in a shift-reduce conflict, shift is selected.

 

Grammar of bison Rewriting Rules (META!)

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

 

How to Combine flex and bison

 

Advantages and Problems of Bottom-Up Parsing

 

Homework: A Calculator for Rational Numbers

Deadline: July 4, 2019 (Thursday in two weeks), 19:00

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

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

Format: Submit the files rationals.lex and rationals.y (in this order!), A4 using BOTH sides (↓↓, not ↓↑), portrait (not landscape), stapled in upper left if more than one page, NO cover page, NO wrapping lines, legible font size, non-proportional font, formatted (indents,...) for easy visibility, name (kanji and kana) and student number as a comment at the top right

Change the simple calculator of calc.y to a calculator that can handle integers and rationals.

Bonus points (発展問題) for dealing with hours, months, years,...

 

Hints for Homework

 

Glossary

unary (operator)
単項 (演算子)
leftmost derivation
最左導出
rightmost derivation
最右導出
reverse order
逆順
lookahead
先読み
non-proportional font
等幅のフォント
rational number
有理数
numerator
分子
denominator
分母
sign
符号
irreducible fraction
既約分数