Skip to content

Instantly share code, notes, and snippets.

@Wonicon
Created October 13, 2015 11:26
Show Gist options
  • Save Wonicon/14edfb51c955c2183650 to your computer and use it in GitHub Desktop.
Save Wonicon/14edfb51c955c2183650 to your computer and use it in GitHub Desktop.
expr parser
%token plus minus mul div lp rp num float
%nonassoc num float
%left plus minus
%left mul div
%right lp rp
%%
exp : exp plus exp
| exp minus exp
| exp mul exp
| exp div exp
| lp exp rp
| num
| float
;
%%
Grammar
0 $accept: exp $end
1 exp: exp plus exp
2 | exp minus exp
3 | exp mul exp
4 | exp div exp
5 | lp exp rp
6 | num
7 | float
Terminals, with rules where they appear
$end (0) 0
error (256)
plus (258) 1
minus (259) 2
mul (260) 3
div (261) 4
lp (262) 5
rp (263) 5
num (264) 6
float (265) 7
Nonterminals, with rules where they appear
$accept (11)
on left: 0
exp (12)
on left: 1 2 3 4 5 6 7, on right: 0 1 2 3 4 5
State 0
0 $accept: . exp $end
lp shift, and go to state 1
num shift, and go to state 2
float shift, and go to state 3
exp go to state 4
State 1
5 exp: lp . exp rp
lp shift, and go to state 1
num shift, and go to state 2
float shift, and go to state 3
exp go to state 5
State 2
6 exp: num .
$default reduce using rule 6 (exp)
State 3
7 exp: float .
$default reduce using rule 7 (exp)
State 4
0 $accept: exp . $end
1 exp: exp . plus exp
2 | exp . minus exp
3 | exp . mul exp
4 | exp . div exp
$end shift, and go to state 6
plus shift, and go to state 7
minus shift, and go to state 8
mul shift, and go to state 9
div shift, and go to state 10
State 5
1 exp: exp . plus exp
2 | exp . minus exp
3 | exp . mul exp
4 | exp . div exp
5 | lp exp . rp
plus shift, and go to state 7
minus shift, and go to state 8
mul shift, and go to state 9
div shift, and go to state 10
rp shift, and go to state 11
State 6
0 $accept: exp $end .
$default accept
State 7
1 exp: exp plus . exp
lp shift, and go to state 1
num shift, and go to state 2
float shift, and go to state 3
exp go to state 12
State 8
2 exp: exp minus . exp
lp shift, and go to state 1
num shift, and go to state 2
float shift, and go to state 3
exp go to state 13
State 9
3 exp: exp mul . exp
lp shift, and go to state 1
num shift, and go to state 2
float shift, and go to state 3
exp go to state 14
State 10
4 exp: exp div . exp
lp shift, and go to state 1
num shift, and go to state 2
float shift, and go to state 3
exp go to state 15
State 11
5 exp: lp exp rp .
$default reduce using rule 5 (exp)
State 12
1 exp: exp . plus exp
1 | exp plus exp .
2 | exp . minus exp
3 | exp . mul exp
4 | exp . div exp
mul shift, and go to state 9
div shift, and go to state 10
$default reduce using rule 1 (exp)
State 13
1 exp: exp . plus exp
2 | exp . minus exp
2 | exp minus exp .
3 | exp . mul exp
4 | exp . div exp
mul shift, and go to state 9
div shift, and go to state 10
$default reduce using rule 2 (exp)
State 14
1 exp: exp . plus exp
2 | exp . minus exp
3 | exp . mul exp
3 | exp mul exp .
4 | exp . div exp
$default reduce using rule 3 (exp)
State 15
1 exp: exp . plus exp
2 | exp . minus exp
3 | exp . mul exp
4 | exp . div exp
4 | exp div exp .
$default reduce using rule 4 (exp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment