The Gateway to Computer Science Excellence
+1 vote
226 views
How will the compiler detect whether "if" is a keyword or an identifier?

Please tell me the concept behind this.
in Compiler Design by (39 points) | 226 views
+2
We are already stores the pattern of each keyword,

then when a lexeme found, then it will be compared by every pattern ( First all keyword patterns, after that only we compare with digits patterns and identifier patterns etc. )
0

@Shaik Masthan

can u pls give an example that describes and differentiate tokens, identifiers and lexemes

+3

first we have patterns for every tokens in the language.

i mean,

identifiers = (letter).(letter+digit+underscore)*

numbers = (digit).(digit)*

comment start = " \* "

each keyword is a separate pattern

 

now, let take the source code :-

int a,b = 50 ;

lexeme :- find the consecutive words until match with a pattern

scans i

scans n

scans t

scans whitespace ===> when it is find a white space then make "int" as a lexeme.

check for pattern which it is matched, then declare it as keyword

 

scans a

scans comma(,) ===> when it is find a comma then make "a" as a lexeme.

check for pattern which it is matched, then declare it as identifier

 

continue this process....

0
0

@Shaik Masthan

Thanks for the valuable comment.

2 Answers

0 votes
keyword is one type of token , so it is already reserved.
by (141 points)
0 votes

In some language like c, java etc keyword is specified so the compiler will detect the keyword in the lexical analysis phase.

While in some language like FORTAN keyword are not specified, it uses lookahead operator ( follow certain pattern before certainty of token for the keyword). These patterns are specified in Lex language (lexical analyzer generator).

Like for keyword the pattern are defined as in LEX language:

  IF / \  ( .*  \ ) { letter}       here for certainty of IF as keyword lookahead  (lookahead operator '/ ' ) certain pattern.

for more information follow book compiler design by ULLMAN.

by Active (1.1k points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,644 questions
56,517 answers
195,583 comments
101,145 users