-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.txt
40 lines (37 loc) · 1.53 KB
/
grammar.txt
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
program ::= {statement}
statement ::= "BREAK" nl
| "FUNC" ident({ident}) nl {statement} "RETURN" {statement} "ENDFUNC" nl
| "PRINT" (expression | string | ident) nl
| "IF" (comparison | ident) "THEN" nl {statement} "ENDIF" nl
| "WHILE" comparison "REPEAT" nl {statement} "ENDWHILE" nl
| "LABEL" ident nl
| "GOTO" ident nl
| {"LET"} ident "=" (expression | char) nl
| "GET" ident "AS" (INT | CHAR | STR) nl
| "OPEN" string "AS" ident nl
| "CLOSE" ident nl
| "READ" (ident | number) FROM (ident | string) INTO ident nl
| "WRITE" (string | ident) INTO (ident | string) nl
comparison ::= expression (("==" | "!=" | ">" | ">=" | "<" | "<=") expression)+
expression ::= {(} term {( "-" | "+" ) term} {)}
term ::= unary {( "/" | "*" ) unary}
unary ::= ["+" | "-"] primary
primary ::= number | char | string | ident
char ::= '(A ... z)'
nl ::= '\n'+
OPEN "file.c" AS fp -> FILE *fp = fopen("file.c", "w+");
CLOSE fp -> fclose(fp);
READ 64 FROM fp INTO file -> fgets(file, 64, fp);
WRITE "hello" INTO fp -> fputs("hello", file.c);
FUNC unimodalmax(list, high, low)
IF low == high THEN
RETURN list[low]
ENDIF
LET mid = floor((high + low) / 2)
IF list[mid] < list[mid + 1]
return unimodalmax(list, mid + 1, high)
ENDIF
IF list[mid] >= list[mid + 1]
return unimodalmax(list, low, mid + 1)
ENDIF
ENDFUNC