Post Reply 
monic part 6: the mC programming language and compiler
12-16-2021, 10:14 AM (This post was last modified: 03-17-2022 01:20 AM by F-73P.)
Post: #4
RE: monic part 6: the mC programming language and compiler
Below is v1.1 of the EBNF grammar, which hopefully solves the problems Rob found in the previous version:

program \(\rightarrow\) code ENDFILE

code \(\rightarrow\) { var-declaration } { function-declaration } main-function-declaration

var-declaration \(\rightarrow\) ARRAY ID dimensions SEMICOLON | [ "*" ] ID SEMICOLON

dimensions \(\rightarrow\) LEFTBRACKET NUM RIGHTBRACKET { LEFTBRACKET NUM RIGHTBRACKET }

function-declaration \(\rightarrow\) ID LEFTPARENTHESIS params RIGHTPARENTHESIS LEFTBRACE local-declarations statement-list RIGHTBRACE

main-function-declaration \(\rightarrow\) MAIN LEFTPARENTHESIS params RIGHTPARENTHESIS LEFTBRACE local-declarations statement-list RIGHTBRACE

params \(\rightarrow\) [ param { COMMA param } ]

param \(\rightarrow\) ID { LEFTBRACKET NUM RIGHTBRACKET } | "*" ID

local-declarations \(\rightarrow\) { LOCAL var-declaration }

statement-list \(\rightarrow\) { statement }

statement \(\rightarrow\) compound-stmt | if-stmt | while-stmt | do-stmt | for-stmt| switch-stmt | return-stmt | read-stmt | write-stmt | assignment-stmt | expression-stmt | goto-stmt | label-stmt | nop-stmt

compound-stmt\(\rightarrow\) LEFTBRACE statement-list RIGHTBRACE

if-stmt \(\rightarrow\) IF LEFTPARENTHESIS expression RIGHTPARENTHESIS statement [ ELSE statement ]

while-stmt \(\rightarrow\) WHILE LEFTPARENTHESIS expression RIGHTPARENTHESIS statement

do-stmt \(\rightarrow\) DO statement WHILE LEFTPARENTHESIS expression RIGHTPARENTHESIS SEMICOLON

for-stmt \(\rightarrow\) FOR LEFTPARENTHESIS [ assignment ] SEMICOLON [ expression ] SEMICOLON [ expression ] RIGHTPARENTHESIS statement

switch-stmt \(\rightarrow\) SWITCH LEFTPARENTHESIS expression RIGHTPARENTHESIS LEFTBRACE { CASE NUM COLON statement-list BREAK SEMICOLON } [ DEFAULT COLON statement-list ] RIGHTBRACE

var \(\rightarrow\) ID { LEFTBRACKET expression RIGHTBRACKET } | "*" ID

return-stmt \(\rightarrow\) RETURN [ expression ] SEMICOLON

read-stmt \(\rightarrow\) READ var SEMICOLON

write-stmt \(\rightarrow\) WRITE expression SEMICOLON

assignment-stmt \(\rightarrow\) assignment SEMICOLON

expression-stmt \(\rightarrow\) [ expression ] SEMICOLON

goto-stmt \(\rightarrow\) GOTO ID SEMICOLON

label-stmt \(\rightarrow\) ID COLON statement-list

nop-stmt \(\rightarrow\) NOPR SEMICOLON

assignment \(\rightarrow\) var "=" expression

expression \(\rightarrow\) simple-expression { boolop simple-expression }

simple-expression \(\rightarrow\) additive-expression [ relop additive-expression ]

additive-expression \(\rightarrow\) term { addop term }

term \(\rightarrow\) [ NEGATE ] factor { mulop [ NEGATE ] factor }

factor \(\rightarrow\) LEFTPARENTHESIS expression RIGHTPARENTHESIS | NUM | var | var "++" | var "--" | "&" ID { LEFTBRACKET expression RIGHTBRACKET } | "true" | "false" | ID LEFTPARENTHESIS args RIGHTPARENTHESIS

args \(\rightarrow\) [ expression { COMMA expression } ]

boolop \(\rightarrow\) "and" | "or" | "not"

relop \(\rightarrow\) "<=" | "<" | ">" | ">=" | "==" | "!="

addop \(\rightarrow\) "+" | "-"

mulop \(\rightarrow\) "*" | "/"

I've written a parser recogniser for this grammar. Below are some screen shots. The source code is displayed in green after "compile" is pressed if no syntax errors are found:

[Image: 51752732405_5b578c8ccc.jpg][Image: 51752503359_5901aebbbb.jpg]

otherwise the offending token is printed in red and an error message displayed at the bottom of the screen:

[Image: 51752731935_1e3857a15e.jpg][Image: 51751029642_31974f540b.jpg]

The C language combines all the power of assembly language with all the ease-of-use of assembly language
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: monic part 6: the mC programming language and compiler - F-73P - 12-16-2021 10:14 AM



User(s) browsing this thread: 3 Guest(s)