在附件中,我的解析器在eclipse中使用javacc生成。我不知道在解析器中处理sintax的错误。 我该怎么办?有什么方法? 如果您可以发布显示它的代码,我更喜欢。 非常感谢你的进步。
/**
* JavaCC template file created by SF JavaCC plugin 1.5.17+ wizard for JavaCC 1.5.0+
*/
options
{
JDK_VERSION = "1.5";
IGNORE_CASE = true;
LOOKAHEAD = 2;
static = true;
}
PARSER_BEGIN(MyParser)
import java.io.* ;
public class MyParser
{
public static void main(String args []) throws ParseException
{
Token t;
MyParserTokenManager tm;
int numToken = 0;
if (args.length<1) {
System.out.println("Sorgente non specificato");
System.exit(1);
}
try {
java.io.InputStream infile;
infile = new java.io.FileInputStream(args[0]);
tm = new MyParserTokenManager(new SimpleCharStream(infile));
}
catch (java.io.FileNotFoundException e) {
System.out.println("File "+args[0]+" non trovato");
return;
} // catch
t = tm.getNextToken();
numToken++;
while(t.kind != MyParserConstants.EOF){
t = tm.getNextToken();
System.out.printf("%s\t %s\n",tokenImage[t.kind],t.image);
numToken++;
}// while
}
static void error_skipto(int kind) {
ParseException e = generateParseException(); // generate the exception object.
System.out.println(e.toString()); // print the error message
Token t;
do {
t = getNextToken();
} while (t.kind != kind);
}
}
PARSER_END(MyParser)
void Stm() :
{}
{
try {
(
SE()
|
MENTRE()
|
DO()
|
PER()
|
condizione()
|
espressione()
|
expres()
)
}catch (ParseException e) {
error_skipto(PV);
}
}
SKIP :
{
" "
| "\r"
| "\t"
| "\n"
}
TOKEN : /* OPERATORS */
{
<PIU: "+">
| <MENO: "-">
| <PER: "*">
| <DIV: "/">
}
TOKEN :
{
<PAR_SIN: "(">
| <PAR_DES: ")">
| <QUAD_SIN: "[">
| <QUAD_DES: "]">
| <UGUALE: "==">
| <VIRGOLA: ",">
| <ASS: "=">
| <NEG: "?=">
| <PV: ";">
| <FOR: "da">
| <TO: "a">
| <IF: "se">
| <ELSE: "altrimenti">
| <END_IF: "fine">
| <WHILE: "mentre">
| <DO: "esegui">
| <COST_INT: (["0"-"9"])+>
| <ID: ["a"-"z","A"-"Z"]>
}
void SE() :
{}
{
(<IF> <QUAD_SIN> condizione() <QUAD_DES> <PAR_SIN> espressione() <PV> <PAR_DES> <END_IF>)
| (<IF> <QUAD_SIN> condizione() <QUAD_DES> <PAR_SIN> espressione() <PV> <PAR_DES> <ELSE> <PAR_SIN>
espressione() <PV> <PAR_DES> <END_IF>)
}
void MENTRE() :
{}
{
<WHILE> <QUAD_SIN> condizione() <QUAD_DES> <PAR_SIN> espressione() <PV> <PAR_DES>
}
void DO() :
{}
{
<DO> <PAR_SIN> espressione() <PV> <PAR_DES> <WHILE> <QUAD_SIN> condizione() <QUAD_DES> <PV>
}
void PER() :
{}
{
<FOR> (<ID>)+ <ASS> <COST_INT> <TO> (<COST_INT>)+ <PAR_SIN> espressione() <PV> <PAR_DES>
}
void condizione():
{ }
{
(<ID>)+ (<NEG> | <UGUALE>) ((<ID>)+ | (<COST_INT>)+)
}
void espressione ():
{}
{
(<ID>)+ <ASS> ((<ID>)+ | (<COST_INT>)+ | expres() )
}
void expres() :
{}
{
((<ID>)+ <PIU> ((<ID>)+ | (<COST_INT>)+))
| ((<ID>)+ <MENO> ((<ID>)+ | (<COST_INT>)+))
| ((<ID>)+ <PER> ((<ID>)+ | (<COST_INT>)+))
| ((<ID>)+ <DIV> ((<ID>)+ | (<COST_INT>)+))
}