Reputation: 51
As part of my project I am supposed to convert EBNF to BNF and use DCG to program BNF in SWI-Prolog.
EBNF is as follows:
program -> int main ( ) { declarations statements }
declarations -> { declaration }
declaration -> type identifier [ [digit] ] ;
type -> int | bool | float | char
statements -> { statement }
statement -> ; | block | assignment | if_statement | while_statement
block -> { statements }
assignment -> identifier [ [digit] ] = expression ;
if_statement -> if ( expression ) statement
while_statement -> while ( expression ) statement
expression -> conjunction { || conjunction }
conjunction -> equality { && equality }
equality -> relation [ equ_op relation ]
equ_op -> == | !=
relation -> addition [ rel_op addition ]
rel_op -> < | <= | > | >=
addition -> term { add_op term }
add_op -> + | -
term -> factor { mul_op factor }
mul_op -> * | / | %
factor -> [ unary_op ] primary
unary_op -> - | !
primary -> identifier [ [digit] ] | literal | ( expression ) | type (
expression )
literal --> digit | boolean
identifier -> A | ... | Z
boolean --> true | false
digit --> 0 | ... | 9
My program should take the source file as input and print a message which says the program is syntactically correct or not. Since I don't have any experience in prolog and watching lots of videos in Youtube and reading tutorials and weblogs which are not helpful at all (at least for me because of lack of experience), I need some help how to do it. Is there anybody please?
Upvotes: 2
Views: 499
Reputation: 51
I solved this question. It was kind of easy:
program --> ["int"], ["main"], ["("], [")"], ["{"], declarations,
statements, ["}"].
declarations --> declaration.
declarations --> declaration, declarations.
declarations --> [].
declaration --> type, identifier, [";"].
declaration --> type, identifier, ["["], digit, ["]"], [";"].
type --> ["int"].
type --> ["bool"].
type --> ["float"].
type --> ["char"].
statements --> statement.
statements --> statement, statements.
statements --> [].
statement --> [";"].
statement --> block.
statement --> assignment.
statement --> if_statement.
statement --> while_statement.
block --> ["{"], statements, ["}"].
assignment --> identifier, ["["], digit, ["]"], ["="], expression, [";"].
if_statement --> ["if"], ["("], expression, [")"], statement.
while_statement --> ["while"], ["("], expression, [")"], statement.
expression --> conjunction, conjunctions.
conjunctions --> ["||"], conjunction.
conjunctions --> ["||"], conjunction, conjunctions.
conjunctions --> [].
conjunction --> equality, equalities.
equalities --> ["&&"], equality.
equalities --> ["&&"], equality, equalities.
equalities --> [].
equality --> relation.
equality --> relation, equ_op, relation.
equ_op --> ["=="].
equ_op --> ["!="].
relation --> addition.
relation --> addition, rel_op, addition.
rel_op --> ["<"].
rel_op --> ["<="].
rel_op --> [">"].
rel_op --> [">="].
addition --> term, terms.
terms --> add_op, term.
terms --> add_op, term, terms.
terms --> [].
add_op --> ["+"].
add_op --> ["-"].
term --> factor, factors.
factors --> mul_op, factor.
factors --> mul_op, factor, factors.
factors --> [].
mul_op --> ["*"].
mul_op --> ["/"].
mul_op --> ["%"].
factor --> primary.
factor --> unary_op, primary.
unary_op --> ["-"].
unary_op --> ["!"].
primary --> identifier.
primary --> identifier, ["["], digit, ["]"].
primary --> literal.
primary --> ["("], expression, [")"].
primary --> type, ["("], expression, [")"].
literal --> digit.
literal --> boolean.
identifier --> ["A"].
identifier --> ["B"].
identifier --> ["C"].
identifier --> ["D"].
identifier --> ["E"].
identifier --> ["F"].
identifier --> ["G"].
identifier --> ["H"].
identifier --> ["I"].
identifier --> ["J"].
identifier --> ["K"].
identifier --> ["L"].
identifier --> ["M"].
identifier --> ["N"].
identifier --> ["O"].
identifier --> ["P"].
identifier --> ["Q"].
identifier --> ["R"].
identifier --> ["S"].
identifier --> ["T"].
identifier --> ["U"].
identifier --> ["V"].
identifier --> ["W"].
identifier --> ["X"].
identifier --> ["Y"].
identifier --> ["Z"].
boolean -->["true"].
boolean --> ["false"].
digit --> ["0"].
digit --> ["1"].
digit --> ["2"].
digit --> ["3"].
digit --> ["4"].
digit --> ["5"].
digit --> ["6"].
digit --> ["7"].
digit --> ["8"].
digit --> ["9"].
Upvotes: 3