Reputation: 1233
Is it possible to use some kind of operator-precedence parser or shunting-yard algorithm for simple programming language? For example, if this language have only expressions, functions and declarations of variables.
What are cons and pros of this way? Can it be much faster than traditional LL/LR parsers?
Upvotes: 4
Views: 1152
Reputation: 1635
To answer your first question, yes it is possible to do operator precedence parsing as part of the language. If you are interested in this you should look into Pratt parsers. This is usually a variant of top-down parsing so it should be in the same performance neighborhood as your other parsing options. Generally, I think people are overly concerned about parsing speed. A compiler is going to spend most of its time doing optimizations usually and sweating a few milliseconds on the parsing stage doesn't seem worth while to me.
There is a language, magpie, that has implemented a Pratt parser. So the big advantage is that they have defined all of the operators for the language in a library instead of the core language. This makes the core language incredibly compact and extensible. The downside though is this leaves other users always having to wonder what the precedence of a particular operator will be as the usual built-in norms may not apply.
Upvotes: 4