-
Notifications
You must be signed in to change notification settings - Fork 0
/
while0parser.jj
93 lines (88 loc) · 1.71 KB
/
while0parser.jj
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
81
82
83
84
85
86
87
88
89
90
91
92
93
PARSER_BEGIN(While0Parser)
import java.io.*;
public class While0Parser
{
/*
http://www.fh-wedel.de/~si/seminare/ws07/Ausarbeitung/04.javacc/praxis.html
http://www.engr.mun.ca/~theo/JavaCC-Tutorial/javacc-tutorial.pdf
*/
public static void main(String args[])
{
try
{
String file_name = "../while0/simple.while0";
FileInputStream fis = new FileInputStream(file_name);
//While0Parser parser = new While0Parser(System.in);
While0Parser parser = new While0Parser(fis);
parser.run();
System.out.println("WHILE0 Programm ok.");
}
catch (Exception e)
{
System.out.println("WHILE0 Programm fehlerhaft.");
System.out.println(e.toString());
}
}
}
PARSER_END(While0Parser)
TOKEN :
{
< IN: "in" >
| < OUT: "out">
| < VAR: "var">
| < ASSIGN: "=" >
| < PLUS: "+" >
| < ZERO: "0" >
| < ONE: "1" >
| < NE: "!=" >
| < WHILE: "while" >
| < DO: "do" >
| < BEGIN: "begin" >
| < END: "end" >
| < SEMICOLON: ";" >
| < COMMA: "," >
| < LPAREN: "(" >
| < RPAREN: ")" >
| < IDENT: (["a"-"z","A"-"Z"] (["0"-"9"])?)+ >
}
SKIP :
{
" " | "\t" | "\n" | "\r" | "\r\n"
}
void run() :
{}
{
<IDENT> <LPAREN> var_in() <SEMICOLON> var_out() <RPAREN> <SEMICOLON>
<VAR> <LPAREN> var_help() <RPAREN> <SEMICOLON>
statement()
}
void var_in() :
{}
{
<IN> <IDENT> (<COMMA> <IDENT>)*
}
void var_out() :
{}
{
<OUT> <IDENT>
}
void var_help() :
{}
{
(<IDENT> (<COMMA> <IDENT>)*)*
}
void statement() :
{}
{
(assign_statement() | while_statement()) (<SEMICOLON> statement())?
}
void assign_statement() :
{}
{
<IDENT> <ASSIGN> (<ZERO> | <IDENT> <PLUS> <ONE>)
}
void while_statement() :
{}
{
<WHILE> <IDENT> <NE> <IDENT> <DO> <BEGIN> statement() <END>
}