
Lex And Yacc Program For Infix To Prefix
Super health club version 2.0. I am supposed to write program to convert infix notation to postfix notation using stacks in Java. I've used lex and yacc (more usually bison) in the past for various projects, usually translators (such as I need to implement infix to postfix conversion algorithm to compute the expression a+b*c-d/e I also. Lex and yacc program to find palindrome string. It is impossible to solve this problem with Yacc. Yacc is a LALR(1) parser generator. LALR refers to a class of grammars. A grammar is a math tool to reason about parsing. One in parens refers to the lookahead - that is a max number of tokens we consider before.
After using the Casio fx-115es Plus, Sharp EL-W516X and TI-36 Pro, I noticed a pattern to its two-number functions like Polar & Rectangular conversion. The form I noticed was func(a,b). In addition, studying the two prefix examples in 'Everything You've Always Wanted To Know About RPN.,' I thought my ideas could be expanded. 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? ( 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. Wow god mode cheats. 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 / and 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.
( 03:08 AM)Les Bell Wrote: 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.