Groups | Search | Server Info | Login | Register


Groups > comp.compilers > #163

How to handle qualified identifiers such as x.y in a Pascal-like language

From noitalmost <noitalmost@cox.net>
Newsgroups comp.compilers
Subject How to handle qualified identifiers such as x.y in a Pascal-like language
Date 2011-06-20 15:43 -0400
Organization Compilers Central
Message-ID <11-06-037@comp.compilers> (permalink)

Show all headers | View raw


Here's a silly example program in my language syntax.
program P:
	var x : int;

	procedure s() :
		var x : int;

		procedure t() :
			var x : int;
			x := 3;
			s.x := x + 10;
		end t;

		x := 2;
		t();
		P.x := x + 10;
	end s;

	x := 1;
	s();
	print x;	# should output 23
end P.


I know how to handle the notation if the qualified identifier is a
record, since there's no change in the stack frame. It's just an
offset in the current frame.

What I don't quite understand is how to parse access to a variable in
an outer stack frame. And I think this is similar to the problem of
nested procedures (which I also don't quite know how to handle).

My parser is recursive descent and produces an AST. The AST consists
of nodes such as Program, ProcDef, VarDef, WhileStmt, AssignStmt,
Ident, Num, and ProcCall. The nodes ProcDef and VarDef are the nodes
in the symbol tables, while Ident represents an identifier as used in
a statement. Thus, an Ident node contains a pointer to a definition in
a symbol table. That definition has a field which gives an offset
relative to the current stack frame. A Program or a ProcDef contains a
symbol table and a list of code statements. The interpreter walks the
AST. When it encounters a node such as ProcDef, it creates a new stack
frame, uses the procedure's symbol table to allocate local variables,
and then walks its list of code statements.

So when I encounter
P.x := x + 10;
in the above program, I get an AST fragment something like
Assign( P.x, Add( x, 10) )
where x is an Ident and 10 is a Num. But what do I do with P.x? Should the
parser do all the work and make P.x simply an Ident (whose symTabEntry points
to the outermost x? Then Ident would need a frameCnt field so the interpreter
knows how many stack frames to back out of. In the above example, frameCnt
would be 1, while reference to a local variable would have frameCnt of 0.

Is this a reasonable way to approach the problem?
[Pretty much.  The standard way to handle references to an enclosing
scope is with a display, the calling procedure passes the enclosing
stack frame addresses as hidden parameters.  If your language doesn't
allow variable sized declarations or recursion, P.x will be at a fixed
distance below the current stack frame so you can address it directly,
but in a more general case, you need the display.  See any 1970s
compiler text for details. -John]

Back to comp.compilers | Previous | NextNext in thread | Find similar


Thread

How to handle qualified identifiers such as x.y in a Pascal-like language noitalmost <noitalmost@cox.net> - 2011-06-20 15:43 -0400
  Re: How to handle qualified identifiers such as x.y in a Pascal-like language torbenm@diku.dk (Torben Ægidius Mogensen) - 2011-06-22 10:57 +0200
  Re: How to handle qualified identifiers such as x.y in a Pascal-like language Hans-Peter Diettrich <DrDiettrich1@aol.com> - 2011-06-22 11:47 +0100
    Re: How to handle qualified identifiers such as x.y in a Pascal-like language George Neuner <gneuner2@comcast.net> - 2011-06-24 18:13 -0400
      Re: How to handle qualified identifiers such as x.y in a Pascal-like language BGB <cr88192@hotmail.com> - 2011-06-29 12:31 -0700
        Re: How to handle qualified identifiers such as x.y in a Pascal-like language BGB <cr88192@hotmail.com> - 2011-07-01 12:46 -0700
        Re: How to handle qualified identifiers such as x.y in a Pascal-like language anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2011-07-02 17:13 +0000
          Re: How to handle qualified identifiers such as x.y in a Pascal-like language BGB <cr88192@hotmail.com> - 2011-07-03 13:14 -0700
            Re: How to handle qualified identifiers such as x.y in a Pascal-like language torbenm@diku.dk (Torben Ægidius Mogensen) - 2011-07-07 10:27 +0200
            Re: How to handle qualified identifiers such as x.y in a Pascal-like language BGB <cr88192@hotmail.com> - 2011-07-07 04:14 -0700
        Re: How to handle qualified identifiers such as x.y in a Pascal-like language George Neuner <gneuner2@comcast.net> - 2011-07-02 16:58 -0400
  Re: How to handle qualified identifiers such as x.y in a Pascal-like language Gene <gene.ressler@gmail.com> - 2011-06-22 19:21 -0700
    Re: How to handle qualified identifiers such as x.y in a Pascal-like language Hans-Peter Diettrich <DrDiettrich1@aol.com> - 2011-06-24 07:56 +0100
      Re: How to handle qualified identifiers such as x.y in a Pascal-like language Gene <gene.ressler@gmail.com> - 2011-06-24 19:19 -0700
        Re: How to handle qualified identifiers such as x.y in a Pascal-like language Hans-Peter Diettrich <DrDiettrich1@aol.com> - 2011-06-25 11:55 +0100
          Re: How to handle qualified identifiers such as x.y in a Pascal-like language noitalmost <noitalmost@cox.net> - 2011-06-29 13:13 -0400
    Re: How to handle qualified identifiers such as x.y in a Pascal-like language "[Linux Magazine]" <uu3kw29sb7@snkmail.com> - 2011-06-24 13:58 +0200
      Re: How to handle qualified identifiers such as x.y in a Pascal-like language George Neuner <gneuner2@comcast.net> - 2011-06-25 17:11 -0400
  Re: How to handle qualified identifiers such as x.y in a Pascal-like language noitalmost <noitalmost@cox.net> - 2011-06-23 12:43 -0400
    Re: How to handle qualified identifiers such as x.y in a Pascal-like language Tony Finch <dot@dotat.at> - 2011-06-29 18:55 +0100
    Re: How to handle qualified identifiers such as x.y in a Pascal-like language BGB <cr88192@hotmail.com> - 2011-06-29 15:51 -0700

csiph-web