1

For a personal project to get me ready for an internship, I have decided to try and learn more about grammars and parsers. I wanted to make a simple compiler, and settled on a grammar that looks like this. Terminals are lowercase.

P -> S P | epsilon
S -> id assign E semi | print id semi
E -> T E'
E'-> + T E' | - T E' | epsilon
T -> F T'
T' -> * F T' | / F T' | epsilon
F -> X F'
F' -> **X F' | epsilon
X -> (E) | identifier | number

The gramamr would allow for code such as this. Operations would be assign

statements(=)

addition(-)

multiplication(*)

division(/)

subtraction(-)

exponentiation(**)

statement sequence

and a print statement(print). Also parantheses would be included.

xabc = 3.141592 ; 
y = -2.0 ; 
z = xabc * y - 1 ; 
print z ;

I was turned on to a program from codeproject.com called Tiny Parser Generator. Here is the link. https://www.codeproject.com/Articles/28294/a-Tiny-Parser-Generator

The simple example given there that allows for the basic mathematical functions on integers I understand. I was just hoping someone could give me some insight in using this generator in trying to transform my grammar into a grammar that would work with the generator, specifically working with variables, the assign statements (i.e. =), and allowing for multiple lines of code to be written. Any help would be appreciated, thanks.

Edit- Thanks for the comments. I realize the confusion created over how I worded my question. The first task I would like to do is to add to the simple expression 2 grammar given by the website tool I linked, the ability to have variables and assignment statements, such as x = 5;

Johnny
  • 33
  • 5
  • This seems like a reasonable post with potentially interesting answers, but can you improve the phrasing of the question at the end? Please see: [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236) – EJoshuaS - Stand with Ukraine Apr 28 '17 at 15:44
  • This is a bit broad of a question for SO. Find a good simple basic parser generator tutorial and work through it. Doesn't matter which parser generator. I've found them all miserable to wrap my head around, so the quality of the tutorial itself is the key point. – 15ee8f99-57ff-4f92-890c-b56153 Apr 28 '17 at 15:44
  • *"allowing for multiple lines of code to be written"* -- I wonder if I'm misunderstanding this part of the question; ordinarily the parser (the syntax part) consumes a stream of tokens from a "lexical analyzer" or "lexer" that consumes text. So the lexer sees "var x =\n9;" and yields something like `[ { "ident": "var" }, {"ident": "x" }, {"op": "="}, {"num": 9 } ]`. The lexer eats and discards whitespace as it goes, unless the language (e.g. JavaScript) wants to treat newlines as significant for some (invariably bad) reason. – 15ee8f99-57ff-4f92-890c-b56153 Apr 28 '17 at 15:55
  • Ok. Thanks for the comments. First task I would like to accomplish is adding in an assignment statement with variables. So I could have a line that says x = 5; I guess that would be a good place for me to start. – Johnny Apr 28 '17 at 16:12
  • I did something like this for a compiler class in university. (I wrote my own generator, but that's not relevant here.) It was... unpleasant. I find generated parsers bulky and difficult to work with. Parse error quality is low, since it has no context (don't underestimate this). As someone who's hand-written a few recursive descent parsers since then, I find they're far simpler and more elegant to work with, and produce better errors and nicer ASTs to boot. And I've seen many hand-written parsers in the workplace, but only one generated one (for an obscure side-feature of a tool). – Cameron Apr 28 '17 at 16:29
  • If you want to just build a recursive descent parser (these work very well for simple grammars like this), see my answer https://stackoverflow.com/questions/2245962/is-there-an-alternative-for-flex-bison-that-is-usable-on-8-bit-embedded-systems/2336769#2336769 – Ira Baxter May 29 '17 at 06:05

0 Answers0