blob: 71fb8d9dcecd22432ee31ad9867d7679e748c518 (
plain) (
blame)
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
|
//
// WRITE A CORRECT GRAMMAR FOR THE TINY LANGUAGE IN THIS FILE.
//
// The grammar is described in data/tinyGrammar.pdf. Do not use dashes
// in your non-terminal names. I recommend you use mixed case naming for your
// non-terminals instead (e.g. stmtSeq).
//
// Do not put any string literals in this grammar (e.g. "="). Use only token
// names given in TinyLexer.tokens.
//
// This file contains an expression grammar that uses tokens described in
// TinyLexer.tokens.
//
// NOTE ONE ERROR in the pdf: the second production rule should read:
// stmt-seq -> stmt-seq stmt | stmt
grammar Tiny;
goal: program;
program: stmtseq;
stmtseq: stmtseq stmt
| stmt;
stmt: ifstmt
| repeatstmt
| assignstmt
| readstmt
| writestmt
;
ifstmt: IF exp THEN stmtseq END
| IF exp THEN stmtseq ELSE stmtseq END
;
repeatstmt: REPEAT stmtseq UNTIL exp;
assignstmt: ID EQUAL exp;
readstmt: READ ID;
writestmt: WRITE exp;
exp: simpleexp LT simpleexp
| simpleexp EQUAL simpleexp
| simpleexp
;
simpleexp: simpleexp PLUS term
| simpleexp MINUS term
| term
;
term: term MULTIPLY factor
| term DIVIDE factor
| factor
;
factor: OPAREN exp CPAREN
| NUM
| ID
;
|