Post Reply 
My own infix to prefix approach. What do you think?
04-29-2014, 03:08 AM
Post: #2
RE: My own infix to prefix approach. What do you think?
(04-29-2014 12:47 AM)Matt Agajanian Wrote:  The way I see it is if I interpret each one or two number operation as func(arg1, arg2...) or func(arg), then, after writing this form of the expression out, I replace the parenthese and commas with spaces for readability and syntax, the end result is a prefix expression.

For example: (2+3)x(4+5) becomes x(+(2,3),+(4,5)).
Thus, replacing parentheses and commas with spaces gives x + 2 3 + 4 5 as the decomposed result.

Do you think my methodology is plausible and valid?

Matt, you're exploring the ideas that underlie parsing of formal languages, finite state machines, compilers, etc. For example, your idea can be implemented very easily using a parser toolkit like ANTLR (Another Toolkit for Language Recognition). In particular, ANTLR will let you write a grammar which will parse infix expressions and spit out prefix notation with virtually no programming at all. In fact, while implementing a parser and interpreter for a domain-specific language I've been working on, my diagnostic dumps of the ANTLR parse trees looked very much like your example. ANTLR also has an associated template engine which allows you to format the output so it could look exactly like your example, with no programming at all.

See http://www.antlr.org/ and http://www.stringtemplate.org/ for more information. If you do decide to investigate this, Terence Parr's books, "The Definitive ANTLR 4 Reference" and especially "Language Implementation Patterns" would get you up to speed very quickly. While ANTLR itself is implemented in Java, it can produce run-times (i.e. parsers to be used in compilers or interpreters) in many other languages.

I've often thought that ANTLR would be an easy way to implement 'cross-compilers' to translate, e.g. BASIC and other languages into RPN or RPL.

--- Les
[http://www.lesbell.com.au]
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: My own infix to prefix approach. What do you think? - Les Bell - 04-29-2014 03:08 AM



User(s) browsing this thread: 1 Guest(s)