(上向き構文解析の原理)
http://www.sw.it.aoyama.ac.jp/2018/Compiler/lecture9.html
© 2005-18 Martin J. Dürst 青山学院大学
bison
Expand the top-down parser of parser1.c to
correctly deal with the four basic arithmetic operations.
(scanner.h
/c
do not change, so no need to submit
them)
(bonus problem) Add more operations to the top-down parser, and/or deal with
parentheses.
(If you solve this problem, also submit the
scanner.h
/c
files, but only one parser.c
file for both problems.)
Factor → FunctionCall | Variable | ArrayElement
FunctionCall → Identifier '(' Parameters ')'
Variable → Identifier
ArrayElement → Identifier '[' Expression ']'
(it is unclear which rule should be selected in Factor
when
the next token is Identifier
)
Depending on parsing method, additional requirements become necessary:
⇒ A grammar that just produces/recognizes 'words' is not enough
⇒ Bottom-up parsing
Parsing Direction |
Top-Down | Bottom-Up |
---|---|---|
Starting point | top (start symbol) | bottom (terminal symbols) |
Parse tree(s) | single tree, some branches incomplete | multiple small trees |
General method | backtracking | dynamic programming (CYK Algorithm) |
Widely used method | recursive descent | LR parsing |
Grammar type used | (E)BNF (incl. repetitions) | no repetitions |
Tools | usually by hand; ANTLR,... | yacc, bison,... |
Main problem | left recursion | shift/... conflicts |
bison
OverviewFiles to start with: makefile, calc.y, calc.lex
make
flex
, bison
, and
gcc
make
command follows the instructions in the
makefile
, executing a minimum of commants to bring everything
up to date make
command to your cygwin
installationmakefile
(→
: tabulator; other
whitespace characters will not work!):target: input1 input2
input3 ...
→command to construct target
make
without arguments will construct the first
target in the makefile
make target
will construct
target
makefile
, without
extensionmakefile.txt
will not work!)bison
Input Format%{
#include <stdio.h>
#define YYSTYPE double
int yylex (void);
void yyerror (char const *);
%}
%token NUM PLUS
%%
statement: exp { printf ("Result is %g\n", $1); }
;
exp: exp PLUS exp { $$ = $1 + $3; }
| NUM { $$ = $1; }
;
%%
int main (void)
{ return yyparse (); }
void yyerror (char const *s)
{ fprintf (stderr, "%s\n", s); }
bison
Input Formatbison 関係の宣言など %{ C 言語の宣言など %}
%%
書換規則 { 実行文 (C 言語) }
書換規則 { 実行文 (C 言語) }
書換規則 { 実行文 (C 言語) }
%%
関数など (C 言語) 関数など (C 言語)
bison
Input FormatMixture of bison
-specific directives and C program fragments
There are three main parts, separated by %%
:
bison
#include
statements, definition/initialization of global
variables,...%{ ... %}
{ ...
}
) that get executed when a rule is matchedNewlines and indentation can be significant
bison
Rewriting Rulesexp: exp PLUS exp { $$ = $1 + $3; }
| NUM { $$ = $1; }
;
:
' (instead of →)|
', terminated by ';
'E0 → E1 '-'
E3
E0
) = S(E1
) -
S(E3
)bison
: $$ = $1 - $3
bison
Manualbison
Programsdiff
commanddiff
, the test is successfulDeadline: June 28, 2017 (Thursday), 19:00
Where to submit: Box in front of room O-529 (building O, 5th floor)
Format: A4 double-sided printout of parser program. Stapled in upper left if more than one page, no cover page, no wrapping lines, legible font size, non-proportional font, portrait (not landscape), formatted (indents,...) for easy visibility, name (kanji and kana) and student number as a comment at the top right
Collaboration: The same rules as for Computer Practice I (計算機実習 I) apply
Complete calc.y so that with test.in as an input, it produces test.check; submit calc.y
only
Hint: If there are differences with newlines, make sure that all files use
Unix line ending convention
(In Notepad2, choose File → Line Endings → Unix (LF))