My own infix to prefix approach. What do you think?
|
04-29-2014, 07:29 AM
Post: #7
|
|||
|
|||
RE: My own infix to prefix approach. What do you think?
(04-29-2014 04:40 AM)Matt Agajanian Wrote: And Les, now that I know thar ANTLR book is a Kindle version, I can read it at my leisure (pun intended). Matt, here's a basic ANTLR grammar that will parse simple algebraic expressions (e.g. it parses your '(2+3)*(4+5)' example perfectly. From this grammar, ANTLR will generate a lexical analyzer, parser and related classes such as a listener that can walk the resultant parse tree and perform operations on it. Code:
And here's a simple Java program that will use the lexer and parser to parse an expression typed into it, and will then print the tree: Code:
I've only just started using ANTLR 4 myself (all my work previously was with ANTLR 3.3) so I haven't gone any further, but I'll probably play around with this example some more when I get time to learn ANTLR 4. Anyway, there's the beginning of some actual code that can parse infix and emit prefix or postfix notation, if you want to play with it. --- Les [http://www.lesbell.com.au] |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)