Principles of Bottom-Up Parsing

(上向き構文解析の原理)

10th lecture, June 23, 2023

Language Theory and Compilers

https://www.sw.it.aoyama.ac.jp/2023/Compiler/lecture10.html

Martin J. Dürst

AGU

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

 

Today's Schedule

 

Last Lecture's Homework

Complete calc.y so that it can be used as a calculator for the four basic arithmetic operations (+-*/), including prefix minus for negative numbers and parentheses for grouping. Create your own test.in for example input, and test.check for example output. Use the grammar to define priorities and associativities (do NOT use %left, %right,...); submit calc.y only.

 

Summary of Last Lecture

 

How to Write a Grammar

 

Grammar Patterns: Priority

(priority: (lowest) low_exp < middle_exp < high_exp (highest); assuming left associative)

low_exp: low_exp low_pri_op middle_exp
| middle_exp
;
middle_exp: middle_exp mid_pri_op high_exp
| high_exp
;

 

How to Express Priorities

 

Grammar Patterns: Associativity

Left associative:

low_exp: low_exp left_assoc_op high_exp
| high_exp
;

Right associative:

low_exp: high_exp right_assoc_op low_exp
| high_exp
;

 

How to Express Associativity

 

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

 

How to Express Repetition (Lists)

 

Grammar Patterns: Parentheses and Unary Expressions

Expressions with parentheses are at the innermost (highest precedence) level (high_exp)

Inside parentheses, any expression can be used, therefore lowest precedence (low_exp)

high_exp: open_paren low_exp close_paren
| literal
;

Unary expressions (e.g. prefix hyphen for negative numbers) are also at the innermost level, but refer to the innermost level on the right hand side, too:

high_exp: HYPHEN high_exp
;

 

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

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

if_statement : IF '(' cond ')' statement
             | IF '(' cond ')' statement
               ELSE statement
;

(statement is a generic term including blocks ({}, compound statements)

 

How bison Works

 

Order of Derivation: Leftmost and Rightmost Derivation

Leftmost derivation: Leftmost nonterminal in syntax tree always expanded first

Rightmost derivation: Rightmost nonterminal in syntax tree always expanded first

Simple example grammar:

E → E '-' T | T
T → n

Example of input: 5 - 7 - 3

 

Derivation Choices

Different choices may generate:

 

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: Debuging

#define YYDEBUG 1 switches on debugging

The output shows how bison works:

 

Understanding bison: The .output File

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

 

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 symbolList
symbol → nonterminalSymbol | terminalSymbol

 

How to Combine flex and bison

 

Advantages and Problems of Bottom-Up Parsing

 

Homework

Deadline: July 6, 2023 (Thursday in two weeks), 22:00

Where to submit: Moodle (rational.y)

Important: This homework requires significantly more time than other homeworks.
Start early, so that you can ask questions next week (June 30, Friday) or in Moodle (Q&A Forum)

Important: Files need to compile with flex/bison/gcc, otherwise this homework will not be counted

Important: Make sure you have #define YYDEBUG 0 in rational.y

Submission: rational.l and rational.y, name (kanji and kana) and student number as a comment at the top right of the file; no overlong (>80 char) lines

Collaboration: The same rules as for
Projects in Information Technology II apply!

 

Homework: A Calculator for Rational Numbers

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

 

Hints for Homework

 

Additional Homework

(no need to submit)

Use calc.output and #define YYDEBUG 1 to understand how bison works

Announcement:

 

Glossary

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