Recently, I am reading Engineering a Compiler 2nd Edition.

But I am struggling to understand the following pseudocode.

enter image description here

Can anyone help me to explain the pseudocode in a simple and plain English?

  • Why NextChar does take a char input?

  • Why lexeme is the result 0f lexeme + char?

  • What is the result of Delta[state, cat];?

  • 2
    $\begingroup$ This is why I don't like pseudocode. With real code, you could look up the meaning in some standard/documentation and/or play around with the code to see what it does. Furthermore, in examples like this, real code in, e.g., Python, would be just as concise and readable. At any rate, the behavior of NextChar isn't determined by this pseudocode. The authors have presumably described what it means in the text or just expect you to infer it from its name. The $\leftarrow$ stands for assignment, that line reassigns the variable lexeme. δ[state,cat] is an 2D array lookup. $\endgroup$ – Derek Elkins left SE Jul 26 '19 at 20:42

Let me answer your questions very quickly:

  • NextChar(char) reads a character and assigns it to the variable char.
  • The new character is appended to the current lexeme being built via the command lexeme = lexeme + char.
  • The table Delta implements the transition function $\delta$ of a DFA (sometimes called an FSM). There is a lot of information on DFAs on the web and even on Computer Science.

I was able to answer these questions via a combination of common sense and a very rudimentary knowledge of how lexers work. More generally, when faced with code or pseudocode which you don't quite understand, you should try to figure out what the code is trying to accomplish, and how. For example, NextChar surely reads a character. You might expect it to return it, but from the context it is clear that it is actually stored in its argument. The fact that it is adjoined to lexeme significantly strengthens this guess.

| cite | improve this answer | |
  • $\begingroup$ Using a pass-by-reference parameter in pseudo code is evil... $\endgroup$ – gigabytes Jul 27 '19 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.