开发者

JLex Scanner Generating

开发者 https://www.devze.com 2023-02-14 13:25 出处:网络
I\'m using JLex for our latest assignment, attempting to generate a scanner for a language given to us by the professor.

I'm using JLex for our latest assignment, attempting to generate a scanner for a language given to us by the professor.

At this point, I have the following written - assume the keywords and identifier rules are correct for the language we're working with.

import java.io.*;
%%
%{
    public static void main(String argv[]) throws java.io.IOException开发者_运维问答
    {
            MyLexer yy= new MyLexer(new FileReader("input"));
            while( yy.yylex() >= 0);
    }
%}

%integer
%class MyLexer
INT =int
KEYWORDS =IF|ELSE|WRITE|READ|RETURN|BEGIN|END|MAIN|INT|REAL
IDENTIFIER =[a-zA-Z_][a-zA-Z0-9_]*
%state COMMENT
%%
{KEYWORDS}
{
    System.out.println("keyword is .. " + yytext());
}
{IDENTIFIER}
{
    System.out.println("ID is .." + yytext());
}


\r\n|.|\n {}

Could anyone give some sort of hint or suggestion on how to: 1. detect comments (of the /* */ format) 2. count each occurrence of identifiers, keywords, etc.


You created the main method, you can also create member variables for you lexer in the lex file:

%{
private int keywordCount = 0;
public static void main(String argv[]) throws java.io.IOException
{
        MyLexer yy= new MyLexer(new FileReader("input"));
        while( yy.yylex() >= 0);
}
%}

Then you can increment keywordCount in the code associated with KEYWORDS:

{KEYWORDS}
{
System.out.println("keyword is .. " + yytext());
++keywordCount;
}

Detecting comments sounds like the main task for this homework assignment, so I'll leave that to you ;)

but I'll tell you that you should look into LEX / JFlex states. You would detect a /* and then transition into a comment state. When you detect */, while in the comment state, you would transition out of the comment state.

0

精彩评论

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