What should lexer output in c do?

#include<stdio.h>

int main()
{
  int a,b;
  a=a+b;
  printf("%d",a);
return 0;
}

what should be the output if this code is passed through lexer

+5
source share
2 answers

lexer simply tokens the stream to turn the stream of characters into a stream of tokens (which will be processed by the parser later to get a complete syntax tree). For your example, you will get something like:

#include <stdio.h> (this is handled by preprocessor, not by lexer so it wouldn't exist)

int KEYWORD
main IDENTIFIER
( LPAR
) RPAR
{ LBRACE
int KEYWORD
a IDENT
, COMMA
b IDENT
; SEMICOL
a IDENT
= ASSIGN
a IDENT
+ PLUS
b IDENT
; SEMICOL
printf IDENT
( LPAR
"%d" STRING
, COMMA
a IDENT
) RPAR
; SEMICOL
return RETURN_KEYWORD
0 INTEGER
; SEMICOL
} RBRACE

Of course, the lexer itself cannot do much, it can simply divide the source into the smallest possible elements, checking syntax errors (for example, misspelled words). You will need something that brings them together to give them meaning.

: (, KEYWORD, ), , RETURN_KEYWORK, IF_KEYWORD ..

+11

, . , #include<stdio.h> stdio.h.

tokens scanner lexical rules, parser, .

+3

All Articles