Monday, December 7, 2009

Compiler Construction: Phase One (Backus–Naur Form)

I had a project of compiler construction a few days ago. It is more interesting than I imagined. There would be a language (suppose it is Simple Programming Language). I need to build the BNF,lexical analyzer and the parser. My friend Wasi built the BNF according to the specifications given to us.

Here is the BNF file

"Name"    = 'SPL'
"Version" = '2009'
"Author"  = ''Mir Sajal'
"About"   = 'Simple SPL'
"Case Sensitive" = False
"Start Symbol" =


<Character>::= {letter}
<Digit>::= {digit}
<CharacterOrDigit>::= {alphanumeric}
NewLine ::= {CR}{LF}|{CR}


<Identifier>::= <Character><CharacterOrDigit>*
<Number>::= <Digit>*|<Digit>+"."<Digit>+
<Negative;>::= "-"
<NumberConstant>::= <Negative><Number>|<Number>
<CharacterConstant>::= "'"<Character>"'"
<Constant>::= <NumberConstant>|<CharacterConstant>
<Value<::= <Identifier>|<Constant>|"("<Expression>")"
<Term>::=^lt;Value>|<Value>>"*"<Value>+|<Value>"/"<Value>+
<Expression>::= <Term>|<Term>"+"<Term>+|<Term>"-"<Term>+
<Comparator>::= "=" | "><" | "<" | ">" | "<=" | ">="
<AndOr>::= AND | OR
<Check>::= [<Not>]<Expression><Comparator><Expression>
<Conditional>::= <Check>|<Check><AndOr><Check>+
<Program> ::= <Identifier>";"<Block>"ENDP"<Identifier>"."
<Block>::= [DECLARATIONS<DeclarationBlock>]CODE<StatementList>
<IdentifierList>::= <Identifier>|<Identifier>","<Identifier>+
<DeclarationBlock>::= <IdentifierList> OF TYPE<Type>";"
<Type>::= CHARACTER | INTEGER | REAL
<StatementList>::= <Statement>|<Statement>";"<Statement>+";"
<Statement>::= <Assignment> | <If> | <Do> | <While> | <Write> | <Read>
<Assignment>::= <Expression> "->" <Identifier>
<If>::= IF <Conditional> THEN <StatementList> ["ELSE" <StatementList>] ENDIF
<Do>::= DO<StatementList>WHILE<Conditional>ENDDO
<While>::= WHILE<Conditional>DO<StatementList>ENDWHILE
<For>::= FOR<Identifier>IS<Expression>BY<Expression>TO<Expression>DO<StatementList>ENDFOR
<Write>::= WRITE<OutputList> | Newline
<Read>::= READ<Identifier>
<OutputList>::= <Value> |<Value>","<Value>+

2 comments:

Portable Storage said...

Good article I think that the project on compiler cosntruction seems pretty interesting.

Thanks,
Portable Storage,
http://moveablecubicle.com/quote.aspx

Reza said...

Thanks