2
votes

I have studied that the parser calls the lexical analyzer and then the lexical analyzer returns the token to it but does this all happen at once or concurrently the lexical analyzer reads the lexemes and returns the token to the parser.

we say that the lexer returns to the parser the token but the lexer stores it in the symbol table so is it that the parser gets the token from the symbol table itself ,but then how does it refer to it?

1
In some compilers the symbol table may not be built in the first phase.So the parser have no choice but to call lexer for the tokens. - Anil Kumar

1 Answers

2
votes

You seem to be misunderstanding what a symbol table is. A lexer transfers a stream of bytes into a stream of lexemes and does not handle the symbol table at all (except when parsing C language, information from the symbol table handled by the parser is fed back to the lexer in the lexer hack: http://en.wikipedia.org/wiki/The_lexer_hack). Handling the symbol table is the job of the parser.

In practice, the lexer may consist of a function which returns the lexical token. So, if the lexer is implemented in that way, the token is passed to the parser as a return value from the lexer function.

You should try using lex/yacc (or flex/bison). Implementing a parser of your own and then seeing what kind of code the tools generate will make you understand how the lexer and the parser are working together. I'm sure there are plenty of good tutorials about lex/yacc on the Internet.