Sorry for such a dumb question, I have to read a file (hello_world.txt) and printline it. I'm trying not to use the HELLO_WORLD token, but im not doing well, so I've comment my attempts. How can i read it using as STR token?
Thanks.
This the simple file.
begin
write "Hello World!"
end
Here is the code:
/**
* JavaCC template file created by SF JavaCC plugin 1.5.28+ wizard for JavaCC 1.5.0+
*/
options
{
static = true;
//DEBUG_PARSER = true;
// DEBUG_TOKEN_MANAGER = true;
}
PARSER_BEGIN(CJComp)
package DefinirSintaxe;
import java.io.*;
public class CJComp
{
public static void main(String args []) throws ParseException
{
CJComp analiser = null;
try {
analiser = new CJComp(new FileInputStream("hello_world.txt"));
analiser.start();
}
catch(FileNotFoundException e)
{
System.out.println("Error: not found");
}
catch(TokenMgrError e) {
System.out.println("Erro léxico\n" + e.getMessage());
}
catch(ParseException e) {
System.out.println("Erro sintático\n" + e.getMessage());
}
}
}
PARSER_END(CJComp)
SKIP : { " " | "\t" | "\r" | "\n" }
SKIP :
{
"//" : COMMEMT_LINE
}
<COMMEMT_LINE>
SKIP:
{
"\n" : DEFAULT | < ~["\n"] >
}
TOKEN :
{
// < STR : (["A"-"Z","a"-"z"])(["A"-"Z","a"-"z","0"-"9"])* > |
// < CMD_BEGIN : ("begin") (< STR >)* | (< CMD_WRITE >) > |
// < CMD_END : (< STR>)* "end" > |
// < CMD_WRITE : (<STR >) * ("write" )(<STR >)* >
< CMD_BEGIN : begin" > |
< CMD_WRITE : "whrite"> |
< CMD_END : "end" > |
< HELLO_WORLD : "Hello World!" >
}
void inicio() :{}
{
( CommandLine() )*
}
void ComandLine():{}
{
( write())
}
void write(): {Token t1 = null, t2 = null, t3 = null, t4 = null;}
{
//t1 = <CMD_BEGIN> <CMD_WRITE> <STR> <CMD_END>
t1 = <CMD_BEGIN>
t2 = <CMD_WRITE>
t3 = <HELLO_WORLD>
t4 = <CMD_END>
{
System.out.println(t3.image) ;
}
}