开发者

Building up a compiler with JFlex/CUP and with some questions

I am currently developing an easy compiler for the language BASIC and I have found some problems which I would like to ask you.

The first one is easy, where I should define my tokens? I mean, in the Lexer.java file I send the tokens to the parser using the expression: return symbol(sym.STATIC); ST开发者_开发问答ATIC is a token for me, but I don't know where to define it.

Second, where I should define the main class in which I create the instances of Lexer and Parser? In the Parser class or in another?

Thanks in advanced.


First: a file with the class defining those symbols will be generated automatically for you

Second: you create your own class with a main where you create an object from the generated Lexer, to which you pass the InputStream, and one from the Parser, to which you pass the Lexer. After you create those two objects you call the method parse() belonging to Parser.


First: You should not be editing the .java file directly. You should be editing the Scanner.jflex file. That should contain the return symbol(sym.STATIC);.

When you use something in the .jflex file, you also need to declare it simultaneously in the Parser.cup file. Below the block that says: parser code {::}, you need to declare the STATIC symbol in the following way: terminal STATIC;

Second: You do not need to create the main file yourself. When using JFLEX and CUP together, you just need to compile the files using the Apache Ant build tool or NetBeans. The main file will automatically be created for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜