A)For Windows

Filename: hello.g4

Scanner || Lexer :

antlr4 file.g4 javac file*.java grun file tokens -tokens

Parser: prog(This is the starting expression of the production rules)

antlr4 file.g4 compile file*.java grun hello prog -gui

1. Sample Lexer Code (lex.g4)

The lexer identifies tokens like keywords, identifiers, and numbers. Here’s an example of a simple lexer grammar.

Lexer Grammar (lex.g4):

lexer grammar lex;

// Tokens
IF: 'if';                       // IF keyword
ID: [a-z]+;                      // Identifier (sequence of lowercase letters)
DIGIT: [0-9]+;                   // Digit (one or more numbers)

// Whitespace - skip spaces, tabs, and newlines
WS: [ \\\\t\\\\r\\\\n]+ -> skip;

Sample Input for Lexer (test_input.txt):

if abc 123

How to Run the Lexer :

  1. Generate the lexer files:

    antlr4 lex.g4
    
    
  2. Compile the lexer files:

    javac lex*.java
    
    
  3. Run the lexer:

    grun lex tokens test_input.txt -tokens
    
    

Expected Output:

[@0,0:1='if',<'if'>,1:0]
[@1,3:5='abc',<ID>,1:3]
[@2,7:9='123',<DIGIT>,1:7]
[@3,10:9='<EOF>',<EOF>,1:10]


2. Sample Parser Code with Embedded Lexer (file.g4)

Here’s an example that combines the lexer and parser into one grammar file. This parser will process simple assignment and if statements.