Search code examples
compiler-construction

How can dfa/nfa recognize the difference between identifiers and keywords?


Should I be considered about identifiers and keywords difference when making the automata?

I could make a general automata and when the automata accept the string I would make it check a table of keywords to check if it is identifier or keyword but I am not sure if this is the right or the best way to do it.

-Update-

I am supposed to make a lexical analyzer for a language that has the following lexemes: Integers, Identifiers, Keywords(‘if’, ‘then’, ‘else’,‘while’) Predicates(‘==’, ‘<’,‘<=’), "=", "+", "(", ")", ";", "}".

I don't need a solution It is just that I am not sure if I understood the concept right..Probably I don't..but I tried researching a bit and still I don't understand.

First finite automatas only accept or reject right? so how can I know when using them what is the lexeme it is accepted as? Did it get accepted as keyword? Identifier? and so on.

If I were to solve this logically I would make my automata have accepting state for each different lexeme then in the implementation I would check which accepted state it ended at, is this how it is supposed to be done? or is an entirely different logic used usually?

If the above assumption is right here comes the problem of my question how would I distinguish between identifiers and keywords? do I do it in the automata? or at the implementation?

If I am not clear just ignore my question I think I need to research more because I am not making sense.


Solution

  • Your approach sounds fine: first check from the table of keywords and if it matches one of these, it's a keyword. If not, it is an identifier.