Discuss Simple interpreter with Lex/YACC in the Other Programming Languages forum on Dev Shed. Simple interpreter with Lex/YACC A place for discussing programming languages not covered in specific forums such as Assembler, COBOL, etc. - you get the idea.
Time spent in forums: 3 Weeks 6 Days 8 h 9 m 2 sec
Reputation Power: 928
Normally you have a parser (possibly constructed with lex/yacc) which reduces the source code to a data structure (usually some sort of tree). You can then execute the source code directly from the structure or convert it into some other form.
For example an if statement might turn into a structure like
Time spent in forums: 4 h 24 m 50 sec
Reputation Power: 0
I came up with a nice solution for the AST, but it requires an OO language:
Have a class Node that contains an eval() function and a list of children. The eval() function would return the value that the node evaluates to.
For example, an IntegerLiteralNode would store its value and its eval() would return that. Then, an AdditionNode can, for example, have two IntegerLiteralNodes as children, and its eval() would call eval() on the two chirdren and return the sum.
I just wonder how would I handle multiple data types? But I'll probably play around with a language containing only integers for now to test my AST concept.