Skip to content

Latest commit

 

History

History
66 lines (36 loc) · 2.13 KB

Lox_Grammar.md

File metadata and controls

66 lines (36 loc) · 2.13 KB

Lox grammar:

programdeclaration* EOF ;

declarationvariableDecl | statement ;

variableDecl"var" IDENTIFIER ("=" expression)? "**;**" ;

statementexprStmt | printStmt | block | ifStmt ;

exprStmtexpression ";" ;

printStmt → print expression ";" ;

block"{" (declaration)* "}" ;

ifStmt"if" "(" expression ")" statement ("else" statement)? ;

whileStmt"while" "(" expression ")" statement ;

forStmt"for" "(" (varDecl | exprStmt) ";" expression? ";" expression? ";" ")" ;

A comma expression evaluates to the final expression

comma exprexpression , (expression)* | "(" expression ")";

expressioncomma_expression | ternary | literal | unary | binary | grouping ;

expressionternary;

ternaryassignment | assignment ? assignment : assignment;

assignmentlogic_or | IDENTIFIER "=" ternary ;

logic_orlogic_and ( "or" logic_and)* ;

logic_andequality ("and" equality)* ;

equalitycomparsion ("==" | "!=" comparison)*;

comparisonterm ("<="|"<"|">"|">=" term)*;

termfactor ("+"|"-" factor)*;

factorunary (( "%" | "/" | "*" ) unary )*;

unary("-" | "!") unary | primary;

primaryliteral | IDENTIFIER | "(" expression ")" ;

literalNUMBER | STRING | "true" | "false" | "nil" ;

grouping"(" expression ")" ;

unary( "-" | "!" ) expression ;

binaryexpression operator expression ;

operator"==" | "!=" | "<" | "<=" | ">" | ">=" | "+" | "-" | "*" | "/" | "%"**;**