Buidling AST with Bison/Flex
Hi friends,
I need to make a simple parser for parsing PHP files (classes, functions et al) and I have read both Flex and Bison manual. I have read somewhere that I must build Abstract Syntax Tree (AST) and utilize that. But I cannot find any tutorial on how to make AST with Bison
Please help me with a link or explanations.
Thanks!
evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
thanks for reply nezachem.
I'm very newbee (actually just starting) with code analysis so take my words with grain of salt! that being said, let say I want to get a class from PHP file and all its Childrens (members as well as attributes) and thereby build a tree I can access. How do I go about?
I have read that tutorial and it is funny to learn how Lexical analyzer works but to be frank, Bison presented me with nightmares (have a lot of complex stuffs but I'm stll trying to grasp). Should I write a new grammar file or just use this one from zend ?
NB: I want to write a source code browser
evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
Thanks for tutorial too, It was one of first things to read on the field and it did help me to understand some stuffs. I will check again!
evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
Feed the PHP code you want to analyze to yyparse(). That would build the tree (unfortunately, you'd be stuck with a global variable to build the tree from; that's a vary annoying shortcoming of bison).
Sorry if I misunderstood the question.
Thanks, just leave PHP thing, suppose you want to parse C++ (which will pretty be a like), how are you going to build AST from that global variable? That is my question where I hit a wall. I can tokenize the file into tokens using flex and I can feed those tokens to a parser but I don't know how to build the tree of those tokens!
evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392