compiler construction - Scanner (Lexing keywords with ANTLR) -


I am working on writing a scanner for my program and a parser with most of the online tutorials included It is not possible to write a lixar without writing a parser at the same time. I'm just trying to generate tokens, they do not interpret them. I want to identify some tokens like INT tokens, float tokens and "start" and "end"

I'm confused about the keyword matching I tried the following attempts to fail:

  keyword: KEY1 | Key 2; KEY1: {input.LT (1) .gettext (). Equal ("BEGIN")}? Letter +; KEY2: {input.LT (1) .gettext (). Equal ("END")}? Letter +; FLOATLITERAL_INTLITERAL: DIGIT + ({2}! = '.}}} = & Gt;'. 'DIGIT * {$ type = FLOATLITERAL;} | {$ type = INTLITERAL;}). '.' DIGIT + {$ type = flotitual}; Piece letter: ('a' .. 'z' | 'a' .. 'z'); Piece points: ('0' .. '9'); Identifier: Letter | Letter digit (letter | digit) + | Letter (letter | DIGIT) *; WS // Whitespace: ('' '\ t' | '' \ n '|' \ r '|' '' f ') + {$ channel = HIDDEN;};    

If you want only a laser, start your grammar like this :

  laser grammar FooLexer; // makes: FooLexer.java   

LT (int): token can only be used on parser rules (A). Inside Lexter rules, you can only use LA (int): int which is next int but there is no need for further manual look. Just do something like this:

  laser grammar FooLexer; BEGIN: 'BEGIN'; END: 'END'; Float: DIEGIT + '.' DIGIT +; INT: DIGIT +; Identifier: Letter (Letter / Figure) *; WS: ('' '\ t' | '' \ n '|' \ r '|' '\ f') + {$ channel = HIDDEN;}; Piece letter: ('a' .. 'z' | 'a' .. 'z'); Piece points: ('0' .. '9');   

I do not need to create a token with the name KEYWORD that matches all keywords: You want to distinguish between BEGIN and END token, right? But if you really want it, then just do it:

  keyword: 'BEGIN' | 'End' ; Remove the   

and BEGIN and END rules. Just make sure that KEYWORD is defined before IDENTIFIER .

EDIT

Test the laser with the following squares:

  import org.antlr. Runtime *; Public square main {public static zero main (string [] args) Exception {String src = "BEGIN END 3.14159 42 FOO"] throws; FoLexer lexer = new FooLexer (new ANTLRStringStream (src)); While (true) {token token = lexer.nextToken (); If (token.getType () == FooLexer.EOF) {break; } System.out.println (token.getType () + "::" + token.getText ()); If you generate a lezzer, then compile the .java source files and run the main square like this:  
  Java-CP antiali3.3 .jar org.antlr.Tool FooLexer.g javac-cp Analyzer3.3.jar *. The following output will be printed in the Java Java-CP: Annealer-3.3.jpg main   

console: 4 :: BEGIN 11 :: 5 :: end 11 :: 7 :: 3.1415 9 11 :: 8 :: 42 11 :: 10 :: FOO

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -