-
Notifications
You must be signed in to change notification settings - Fork 0
/
syn.y
80 lines (71 loc) · 1.39 KB
/
syn.y
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
68
69
70
71
72
73
74
75
76
77
78
79
80
%{
package main
import (
"github.com/timtadh/lexmachine"
)
%}
%union{
token *lexmachine.Token
ast *Node
}
%token BANG
%token INTEGER_LITERAL
%token BOOLEAN_LITERAL
%token IDENTIFIER
%token CLASS
%token PUBLIC
%token STATIC
%token VOID
%token MAIN
%token STRING
%token SYSTEMOUTPRINTLN
%token RETURN
%token INT
%token IF
%token FOR
%token ELSE
%token WHILE
%token THIS
%token NEW
%token BOOLEAN
%token LENGTH
%token EXTENDS
%token LEFTBRACKET
%token RIGHTBRACKET
%token LEFTANGLEBRACKET
%token RIGHTANGLEBRACKET
%token COMMA
%token SEMICOLON
%token COLON
%token LEFTPARENTHESIS
%token RIGHTPARENTHESIS
%token PERIOD
%token PLUS
%token ASTERIX
%token DIVISION
%token MODULO
%token DOUBLEQUAL
%token DIFFERENT
%token LESS
%token LESSOREQUALS
%token GREATER
%token GREATEROREQUALS
%token LOGICALAND
%token LOGICALOR
%token EQUAL
%token MINUS
%%
Program : Statement {yylex.(*golex).stmts=append(yylex.(*golex).stmts,$1.ast)}
;
Statement: Expression Operator Expression
{
$$.ast = NewNode("Statement", nil).AddKid($2.ast).AddKid($1.ast).AddKid($3.ast)
__yyfmt__.Printf("%s + %s",$1.token.Lexeme,$3.token.Lexeme)
}
;
Expression: INTEGER_LITERAL {$$.ast = NewNode("INTEGER_LITERAL",$1.token)}
;
Operator: PLUS {$$.ast = NewNode("PLUS", $1.token)}
| MINUS {$$.ast = NewNode("MINUS", $1.token)}
;
%%