Re: Syntax Tree Construction

Martin Ward <Martin.Ward@durham.ac.uk>
14 Sep 2003 17:00:28 -0400

          From comp.compilers

Related articles
Sytnax Tree Construction chris@labino.net (2003-09-04)
Re: Syntax Tree Construction Martin.Ward@durham.ac.uk (Martin Ward) (2003-09-14)
Re:Syntax Tree Construction robert.thorpe@antenova.com (Rob Thorpe) (2003-09-14)
| List of all articles for this month |
From: Martin Ward <Martin.Ward@durham.ac.uk>
Newsgroups: comp.compilers
Date: 14 Sep 2003 17:00:28 -0400
Organization: Compilers Central
References: 03-09-024
Keywords: parse
Posted-Date: 14 Sep 2003 17:00:28 EDT

> But for instance, how do you handle the actual language keywords. For
> instance, how would a tree be build for the BASIC code:
>
> DO
> x=x+2
> IF x=12
> PRINT "x=12"
> ELSEIF x=14
> PRINT "x=14"
> ELSE CALL function()
> ENDIF
> LOOP WHILE x<>20


Here's how the FermaT transformation system represents a syntax tree.


The following MetaWSL program parses some WSL code
and prints the syntax tree:


@New_Program(FILL Statements
WHILE x<>20 DO
   x:=x+2;
IF x=12
THEN PRINT("x=12")
ELSIF x=14
THEN PRINT("x=14")
ELSE @function() FI OD
ENDFILL);
@Print_WSL(@Program, "")


Here is the output it produces:


Statements
      While
      : Not_Equal
      : Variable x
      : Number 20
      : Statements
      : Assignment
      : : Assign
      : : Var_Lvalue x
      : : Plus
      : : Variable x
      : : Number 2
      : Cond
      : : Guarded
      : : Equal
      : : Variable x
      : : Number 12
      : : Statements
      : : Print
      : : : Expressions
      : : : String x=12
      : : Guarded
      : : Equal
      : : Variable x
      : : Number 14
      : : Statements
      : : Print
      : : : Expressions
      : : : String x=14
      : : Guarded
      : : True
      : : Statements
      : : MW_Proc_Call
      : : : Name @function
      : : : Expressions
      : : : Lvalues


The WHILE statement has two components: a condition (Not_Equal in this case)
and a statement sequence. The IF statement is called a Cond and has
a variable number of Guarded components (if the last Guarded has
True as its first component, then it is displayed as an ELSE clause).
And so on...


FermaT can be downloaded from:
http://www.cse.dmu.ac.uk/~mward/fermat.html


--
Martin


Martin.Ward@durham.ac.uk http://www.cse.dmu.ac.uk/~mward/ Erdos number: 4
G.K.Chesterton web site: http://www.cse.dmu.ac.uk/~mward/gkc/


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.