-
Notifications
You must be signed in to change notification settings - Fork 107
bnf
陳鍾誠 edited this page Feb 24, 2019
·
9 revisions
E=T ([+-] T)*
T=F ([*/] F)*
F=Number | Id | '(' E ')'
String = ".*"
Number = [0-9]+
Id = [A-Z_][A-Z0-9_]*
Char = .
PROG = STMT
STMT = WHILE | BLOCK | ASSIGN
WHILE = while (E) STMT
BLOCK = { STMT* }
ASSIGN = id '=' E;
E = F (op E)*
F = (E) | Number | Id
PROG = (INCLUDE | DECL | FUNCTION)*
INCLUDE = #...>
DECL = *? ASSIGN
FUNCTION = type id (PARAM_LIST) BLOCK
STMT = WHILE | IF | BLOCK | RETURN | VAR ; | ASSIGN;
WHILE = while (E) STMT
IF = if (E) STMT (else STMT)?
BLOCK = { LIST<VAR> STMT* }
RETURN = return E;
ASSIGN = P (=E)?
CALL(id) = id ( LIST<E> )
VAR = Type LIST<DECL>
E = F (op F)*
F = (++ | -- | [*&+-~!])? P
P = ((E) | Number | Literal | ID) ([E] | (LIST<E>) | ++ | --) // ID (LIST<E>) == CALL(id)
chunk ::= {stat [`;´]} [laststat [`;´]]
block ::= chunk
stat ::= varlist `=´ explist |
functioncall |
do block end |
while exp do block end |
repeat block until exp |
if exp then block {elseif exp then block} [else block] end |
for Name `=´ exp `,´ exp [`,´ exp] do block end |
for namelist in explist do block end |
function funcname funcbody |
local function Name funcbody |
local namelist [`=´ explist]
laststat ::= return [explist] | break
funcname ::= Name {`.´ Name} [`:´ Name]
varlist ::= var {`,´ var}
var ::= Name | prefixexp `[´ exp `]´ | prefixexp `.´ Name
namelist ::= Name {`,´ Name}
explist ::= {exp `,´} exp
exp ::= nil | false | true | Number | String | `...´ | function |
prefixexp | tableconstructor | exp binop exp | unop exp
prefixexp ::= var | functioncall | `(´ exp `)´
functioncall ::= prefixexp args | prefixexp `:´ Name args
args ::= `(´ [explist] `)´ | tableconstructor | String
function ::= function funcbody
funcbody ::= `(´ [parlist] `)´ block end
parlist ::= namelist [`,´ `...´] | `...´
tableconstructor ::= `{´ [fieldlist] `}´
fieldlist ::= field {fieldsep field} [fieldsep]
field ::= `[´ exp `]´ `=´ exp | Name `=´ exp | exp
fieldsep ::= `,´ | `;´
binop ::= `+´ | `-´ | `*´ | `/´ | `^´ | `%´ | `..´ |
`<´ | `<=´ | `>´ | `>=´ | `==´ | `~=´ |
and | or
unop ::= `-´ | not | `#´
class : 'class' id '{' classVarDec* subroutineDec* '}'
classVarDec: {'static'|'field'} type varList ';'
varList: varName (',' varName)*
varName: ID
subroutineDec: ('constructor'|'function'|'method') ('void'|type) ID '(' parameterList? ')' subroutineBody
parameterList: (parameter (',' parameter)*)?
parameter: type varName
subroutineBody: '{' varDec* statements '}'
varDec: 'var' type varList ';'
statements : statement*
statement: letStatement | ifStatement | whileStatement | doStatement | returnStatement
letStatement: 'let' varName ( arraySubscript )? '=' expression ';'
arraySubscript: '[' expression ']'
block = '{' statements '}'
ifStatement : 'if' '(' expression ')' block '('else' block)?
whileStatement: 'while' '(' expression ')' block
doStatement: 'do' call ';'
returnStatement: 'return' expression? ';'
expression: term (op term)*
term: integerConstant | stringConstant | keywordConstant | varName arraySubscript?| call | '(' expression ')' | op1 term
call: subroutineName '(' expressionList ')' | (className | varName) '.' subroutineName '(' expressionList ')'
expressionList: (expression (',' expression)*)?