Musings, ramblings, rantings about technology, games, puzzles, and whatever else catches my attention.
Monday, August 25, 2014
PLY lexing and parsing
Having not posted for a while, it's overdue to report some progress here. There's not much in the way of visuals, but the above "screenshot" shows what I've been up to - I've been working on writing my own language, which has a very C-like feel to it. There are functions and curly braces, so nothing too exciting there.
I think that I want to add multiple return values, something like this:
(boolean, uint) find (string x, char c);
This would declare a function called "find" which takes a string and a character and returns a boolean and an unsigned integer - the boolean would indicate if the character was found, and the integer would say where.
I know that there are ways to accomplish this through magic numbers, like allowing the function to return a signed value and then returning -1, but that's a little clumsy for this example, and there are cases where it's even worse.
The bit that I'm showing off here is that, using PLY, a Python Lex/Yacc implementation, I've got code that recognizes a factorial function, shown in the screenshot. (As opposed to issuing errors on language use that it doesn't recognize, ie 'syntax errors'.) It doesn't build an Abstract Syntax Tree (AST) yet, but the productions are in place, and it's relatively easy (or so the PLY code tells me) to build up a tree from what I have.
One thing I'm discovering is that it's tricky to debug errors in language grammar specifications.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment