0
$\begingroup$

I would like to know how a single pass compiler does parsing,analyzing and generating code all at once as said in P.14 of SIngle Pass Compilers.Is it possible to do all these at once since you can't generate an code without parsing or analyzing it which should be done earlier before producing the object code?

$\endgroup$
  • 1
    $\begingroup$ "you can't generate an code without parsing": but you can generate code simultaneously, provided you collected sufficient information. $\endgroup$ – Yves Daoust Oct 27 '15 at 15:58
-1
$\begingroup$
PROGRAM Demo
{
  INTEGER i;
  i= 4;
  WRITELN(i + 5);
}.
  • Line 1: enter the program name in the symbol table. Generate the program prologue.
  • Line 3: enter the declaration of variable i in the symbol table.
  • Line 4: evaluate the constant expression and generate code for the initialization of i.
  • Line 5: parse the function call and the argument expression. Generate code for the evaluation of the expression i + 5, the push on the stack and the call of the WRITELN predefined function.
  • Line 6: Generate the program epilogue.

Done.

$\endgroup$
  • $\begingroup$ That doesn't really get into the real challenges of single pass compilers, namely implicit definitions (For instance, a forward jump to a label. You can generally do this, but it's a pain, especially if you're sticking with strictly append-only output.) and grammatical issues (if you can't parse the language with a fixed overhead, e.g. C++). $\endgroup$ – TLW Oct 27 '15 at 22:16
  • $\begingroup$ @TLW The goal of the answer was to show that it is possible, not to show that it is "evil". I disapprove the downvote. By the way, a forward jump raises no special issue. When the goto is met, you enter the label in the symbol table and generate code to jump to it. Actual address computation will be resolved by the linker. Obviously, the grammar must be single-pass parseable. Single-pass compilers for real-world languages do exist. $\endgroup$ – Yves Daoust Oct 28 '15 at 8:41
  • $\begingroup$ As I said, "you can generally do this, but it's a pain". In particular, that means you're emitting an indirect jump for every single forward jump, which isn't exactly efficient. $\endgroup$ – TLW Oct 28 '15 at 11:26
  • $\begingroup$ @TLW: I don't see why an indirect jump. The code needn't be different from the one generated multipass. $\endgroup$ – Yves Daoust Oct 28 '15 at 11:30
  • $\begingroup$ You see the input "if (x) { start of long block of code...". In an ordinary compiler you'd compile that (roughly) as a conditional jump to the end of the long block of code. In a single pass compiler, however, you can't emit the conditional jump, as you don't know the offset to output. (And note that the linker/assembler is generally considered a compiler pass, so punting it off to the linker/assembler isn't allowed either). $\endgroup$ – TLW Oct 28 '15 at 16:08

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.