Last active
September 5, 2019 22:00
-
-
Save jahan-addison/124e3c87959c18fd3346ec35848cbdfb to your computer and use it in GitHub Desktop.
B Language LALR(1) EBNF-like grammar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///////////////////////////////////////////////////////// | |
// B Language Grammar | |
// Author: Jahan Addison | |
// LALR(1) EBNF-like Grammar | |
// Placed under CC0 1.0 | |
///////////////////////////////////////////////////////// | |
program : ( definition ) * | |
definition : function_definition | |
| vector_definition | |
vector_definition : NAME ( "[" ( constant ) ? "]" ) ? (ival ( ","? ival ) * )? TERMINATE | |
function_definition : ( NAME "(" parameters ")" block_statement ) | |
parameters : [ rvalue (( "," rvalue ) *) ] | |
ival : constant | |
| NAME | |
statement : block_statement | |
| label_statement | |
| auto_statement | |
| extrn_statement | |
| case_statement | |
| if_statement | |
| while_statement | |
| switch_statement | |
| break_statement | |
| goto_statement | |
| return_statement | |
| rvalue_statement | |
auto_statement : "auto" lvalue ("," lvalue )* TERMINATE [ statement ] | |
extrn_statement : "extrn" NAME ("," NAME)* TERMINATE [ statement ] | |
case_statement : "case" constant ":" (statement +) | |
label_statement : /[a-zA-Z_][a-z._A-Z0-9]{0,7}[\s]*:/ (statement *) | |
goto_statement : "goto" NAME TERMINATE | |
if_statement : "if" "(" rvalue ")" statement [ "else" statement ] | |
return_statement : "return" [ ( "(" rvalue ")" ) ] TERMINATE | |
| "return" TERMINATE | |
rvalue_statement : expression + | |
block_statement : ( "{" (statement) * "}" ) | |
while_statement : "while" "(" ( rvalue ) ")" statement | |
switch_statement : "switch" ("(" rvalue ")" | rvalue) "{" case_statement * "}" | |
break_statement : "break" TERMINATE | |
expression: TERMINATE | |
| rvalue TERMINATE | |
rvalue : evaluated_expression | |
| assignment_expression | |
| pre_inc_dec_expression | |
| post_inc_dec_expression | |
| unary_expression | |
| unary_address_of | |
| relation_expression | |
| ternary_expression | |
| function_expression | |
| constant_expression | |
| lvalue_expression | |
function_expression : lvalue "(" parameters ")" | |
ternary_expression : rvalue "?" rvalue ":" rvalue | |
relation_expression : rvalue binary rvalue | |
address_of_expression : unary_address_of lvalue | |
unary_expression : unary rvalue | |
post_inc_dec_expression : lvalue inc_dec | |
pre_inc_dec_expression : inc_dec lvalue | |
assignment_expression : lvalue assignment_operator rvalue | |
constant_expression : constant | |
evaluated_expression : "(" rvalue ")" | |
lvalue_expression : lvalue | |
?lvalue: NAME -> identifier | |
| unary_indirection rvalue -> indirect_identifier | |
| lvalue "[" rvalue "]" -> vector_identifier | |
assignment_operator : EQUAL [ binary ] | |
?inc_dec : unary_inc | |
| unary_dec | |
?unary : unary_minus | |
| unary_not | |
| unary_ones_complement | |
unary_indirection : "*" | |
unary_address_of : "&" | |
unary_minus : "-" | |
unary_not : "!" | |
unary_inc : "++" | |
unary_dec : "--" | |
unary_ones_complement : "~" | |
?binary : "|" -> or_operator | |
| "&" -> and_operator | |
| "==" -> eq_operator | |
| "!=" -> neq_oeprator | |
| "<" -> lt_operator | |
| "<=" -> lte_operator | |
| ">" -> gt_operator | |
| ">=" -> gte_operator | |
| "^" -> xor_operator | |
| "<<" -> lshift_operator | |
| ">>" -> rshift_operator | |
| "-" -> sub_operator | |
| "+" -> add_operator | |
| "%" -> mod_operator | |
| "*" -> mul_operator | |
| "/" -> div_operator | |
constant : /[0-9]/ + -> number_literal | |
| "'" ( CHAR ) ~ 1 .. 4 "'" -> constant_literal | |
| "' '" -> constant_literal | |
| /"(\\\"|\\\\|[^"\n])*?"i?/ -> string_literal | |
EQUAL : "=" | |
TERMINATE : ";" | |
NAME : /[a-zA-Z_][a-z._A-Z0-9]{0,7}/ | |
CHAR : /[\x00-\x7F]/ | |
COMMENT : "/*" /(\S|\s)*?/ "*/" | |
WHITESPACE : /[ \t\f\r\n]/+ | |
%ignore WHITESPACE // Ignore whitespace | |
%ignore COMMENT // Ignore comments |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment