Table of Contents

BNF Grammar

Introduction

This is a BNF Grammar compatiable with Gold Parser for LOLCode. Gold Parser is pretty cool and has 'Engine' implimentations in plenty of languages, including but not limited to C#, C, Python and Java. An engine will take an compile grammar and should give you a funky parse tree, from this you could generate ASM, MSIL or 'some other language' as the output (which would require a bit of work mind you).

Currently it can parse Eko's recommendation example. However there are likely to be issues with this grammar, if you find any please make updates and/or let me know.

The Grammar

"Name"     	= 'LOLCODE'
"Author"   	= 'Phil Price'
"Version"  	= '1.0Rec'
"About"    	= 'www.lolcode.com'

"Start Symbol" 	= <Start>

! ------------------------------------------------- Sets

{WS}		= {Whitespace} - {CR} - {LF}
{ID Head}	= {Letter} + [_]
{ID Tail}	= {Alphanumeric} + [_]
{String Chars}	= {Printable} + {HT} - ["]

! ------------------------------------------------- Terminals

Whitespace	= {WS}+
NewLine		= {CR}{LF} | {CR} | {LF}

Identifier	= {ID Head}{ID Tail}*
StringLiteral	= '"' {String Chars}* '"'
IntLiteral	= {digit}+ 
Comment		= BTW{Space}{Printable}*

! ------------------------------------------------- Statement seperators

<nl>     	::= NewLine <nl>          !One or more
		|  NewLine

<nlp>		::= NewLine <nl>          !One or more
		|  NewLine
		| '.' <nl>
		| '.'

<nl Opt>	::= NewLine <nl Opt>      !Zero or more
          	|  !Empty

!=======================================================

! ------------------------------------------------- Program

<Start>		::= <nl opt> <Program> 
<Program>	::= 'HAI' <nl> <Statements> KTHXBYE <nl Opt>

! ------------------------------------------------- Keyword Lists

<ReadOp>	::= 'WORD' 
		| 'LINE' 
		| 'LETTAR' 
		| !Nothing

<MathOp>	::= 'UPZ' 
		| 'NERFZ' 
		| 'TIEMZD'
		| 'OVARZ'

<DieOp>		::= 'BYES' 
		| 'DIAF' 

!=======================================================

<Statements>	::= <Statement><nlp> <Statements>
		| !Empty

<Statement>	::= <Import>
		| <Write>
		| <Read>
		| <VariableDecl>
		| <VariableAssi>
		| <Die>
		| <Conditional>
		| <Loop>
		| <Break>
		| <Math>
		| Comment

<Import>	::= 'CAN HAS' Identifier '?' 
		| 'CAN HAS' StringLiteral '?' 

<Write>		::= 'VISIBLE' <Expression>
		| 'VISIBLE' <Expression>'!'

<VariableDecl>	::= 'I HAS A' Identifier
		|  'I HAS A' Identifier ITZ <Expression> ! This should actually work according the langue definitons

<VariableAssi>	::= 'LOL' <Variable> 'R' <Expression>

<Read>		::= 'GIMMEH' <ReadOp> Identifier

<Die>		::= <DieOp>
		| <DieOp> IntLiteral
		| <DieOp> IntLiteral StringLiteral

<Conditional>	::= 'IZ' <Expression><QOpt> <nlp>'YARLY'<nlp> <Statements> <ConditionElse> 'KTHX'
		| 'IZ' <Expression><QOpt> <nlp> <Statements> 'KTHX'
			 
<ConditionElse>	::= 'NOWAI'<nlp> <Statements> | ! Nothing

<Loop>		::= 'IM IN YR' Identifier <nlp> <Statements> 'KTHX'

<Break>		::= 'GTFO'

<Math>		::= <MathOp> <Value>'!!'
		| <MathOp> <Value>'!!'<Expression>

<QOpt>			::= '?' | !Nothing

! ---- Portions of the following block are based on Basic-64.grm from devincook.com/goldparaser

<Expression>	::= <And Exp> OR <Expression> 
		| <And Exp> 

<And Exp>	::= <Not Exp> AND <And Exp> 
		| <Not Exp> 
 
<Not Exp>	::= NOT <Compare Exp> 
		| <Compare Exp> 

<Compare Exp>	::= <Add Exp> 'LIEK'  <Compare Exp> 
		| <Add Exp> 'BIGR THAN' <Compare Exp> 
		| <Add Exp> 'SMALR THAN' <Compare Exp> 
		| <Add Exp> 

<Add Exp>	::= <Mult Exp> 'UP' <Add Exp> 
		| <Mult Exp> 'NERF' <Add Exp> 
		| <Mult Exp> 

<Mult Exp>	::= <Negate Exp> 'TIEMZ' <Mult Exp> 
		| <Negate Exp> 'OVAR' <Mult Exp> 
		| <Negate Exp> 

<Negate Exp>	::= '-' <Literal>
		| <Literal>  
		| '-' <Variable>
		| <Variable>

<Literal>	::= IntLiteral 
		| StringLiteral 

<Variable>	::= Identifier 
		| <Variable> IN MAH <Expression> 

<Value>		::= <Literal> 
		| <Variable> 

Changes

Feedback