-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInfix.eyp.rep
67 lines (62 loc) · 1.43 KB
/
Infix.eyp.rep
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#%semantic token '*' '='
%right '='
%left '-' '+'
%left '*' '/'
%left NEG
#%tree
%tree bypass
%%
line:
sts <%name EXPS + ';'>
;
sts:
%name PRINT
PRINT leftvalue
| exp
;
exp:
%name NUM NUM
| %name VAR VAR
| %name ASSIGN leftvalue '=' exp
| %name PLUS exp '+' exp
| %name MINUS exp '-' exp
| %name TIMES exp '*' exp
| %name DIV exp '/' exp
| %no bypass NEG
#| %name NEG
'-' exp %prec NEG
| '(' exp ')'
;
leftvalue : %name VAR VAR
;
%%
my $lineno = 1;
sub Err {
my $parser = shift;
my($token)=$parser->YYCurval;
my($what)= $token ? "input: '$token'"
: "end of input";
my @expected = $parser->YYExpect();
local $" = ', ';
die << "ERRMSG";
Syntax error near $what (line number $lineno).
Expected one of these terminals: @expected
ERRMSG
}
sub Lex {
my($parser)=shift; # The parser object
for ($parser->YYData->{INPUT}) { # Topicalize
m{\G[ \t]*}gc;
m{\G\n}gc
and $lineno++;
m{\G([0-9]+(?:\.[0-9]+)?)}gc
and return('NUM',$1);
m{\Gprint}gc
and return('PRINT', 'PRINT');
m{\G([A-Za-z_][A-Za-z0-9_]*)}gc
and return('VAR',$1);
m{\G(.)}gc
and return($1,$1);
return('',undef);
}
}