Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Compiler noob question

Name: Anonymous 2009-10-31 0:27

hai.  I'm trying to write a compiler for the first time (just a relatively simple toy language I thought up) and I'm not sure exactly how I should store the parse tree in memory.  I'm writing the compiler in C.  Any suggestions?

Name: Anonymous 2009-10-31 0:41

What you want is an AST(Abstract Syntax Tree). Each node of the tree has a type representing some instruction, and a list of parameters. Each parameter can be another node and so on.

A simpler way to explain this would be through a Lisp list:

1 + 2 * 3 => (+ 1 (* 2 3))

the first element of the list represents the instruction, while the rest are parameters.

Other decent representations can be Recursive variants in ML/Ocaml (Haskell has them too):

type Arith_AST =
  Add of Expr * Expr |
  Sub of Expr * Expr |
  Mul of Expr * Expr |
  Div of Expr * Expr |
  Not of Expr;;
 
type Expr =
  Expr of Arith_AST |
  Int of int;;


How you choose to represent these structures in C is up to you, it's easy if you understand how it works in an abstract manner as shown in the Lisp and O'Caml examples.

Name: Anonymous 2009-10-31 0:44

>>2
YOU HELPED HIM!!!

Name: Anonymous 2009-10-31 1:07

>>3
Oh damn... at least I didn't spoonfeed him one of the many concrete representations possible in C, he'll have to figure out those trivialities himself.

Name: Anonymous 2009-10-31 1:08

...or he could use a language that is more suitable for writing compilers.

Name: Anonymous 2009-10-31 1:48

>>5
Don't worry, he won't.

Name: Anonymous 2009-10-31 1:51

Check out Lex/Flex and Yacc/Bison.

Name: Anonymous 2009-10-31 1:54

>>2,4
yeah I understand that much about parser trees (although I thought it would better to use a concrete syntax tree rather than an AST.  do you disagree?), I'm just not sure exactly how I should represent all of that in memory.  right now I'm thinking on how a node could have an unlimited number of direct child nodes.

>>7
I want to do it by hand!

Name: Anonymous 2009-10-31 2:22

>>7
And then rip your hair out dealing with shift/reduce conflicts.

Try something less shit. Just because existing software uses those doesn't mean anything; there's far far better choices.

Name: Anonymous 2009-10-31 2:22

>>6
Did you mean to say FrozenVoid detected?

Name: Anonymous 2009-10-31 2:41

right now I'm thinking on how a node could have an unlimited number of direct child nodes.
Linked list of childer or growable arrays or <whatever_the_hell_you_want>

Name: Anonymous 2009-10-31 3:06

A linked list would be simplest, but more specialized data structures work well too.

Name: Anonymous 2009-10-31 3:44

>>11
k, I had thought a linked list wouldn't work some reason but I realize there's nothing wrong with it

sage for /thread

Name: Anonymous 2009-10-31 5:07

>>1
I'm trying to write a compiler for the first time
4chan is 18+, 12 year olds are not welcome.

Name: Anonymous 2009-10-31 6:12

Read SICP THE DRAGON BOOK

Name: Anonymous 2009-10-31 7:03

>>9
what the
how does parser generator software relate to number of shift/reduce and reduce/reduce conflicts in grammar?

Name: Anonymous 2009-10-31 8:49

>>15
I hear mixed things about The Dragon Book

Name: Anonymous 2009-10-31 9:20

>>15
[spoiler][b][i][u][o]V[sup]I[sub]P QUALITY[/o][/u][/i][/b][/spoiler]

Name: Anonymous 2009-10-31 9:25

VIP QUALITYI

Name: Anonymous 2009-10-31 11:00

Can't be done unless you're 12 years old.

Name: Anonymous 2009-10-31 11:19

[spoiler][b][i][u][o]V[sup]I[sub]P QUALITY[/o][/u][/i][/b][/spoiler]

Name: Anonymous 2009-10-31 11:27

>>18,21
If you cannot write it properly on your first try, please validate your BBCOEDS sirs!

Name: Anonymous 2009-10-31 11:56

cHOT STRIPPING BBCOEDS

Name: Anonymous 2009-10-31 13:36

>>9

Maybe if you're fucking retarded, and don't know how to properly write a grammar.

Name: Anonymous 2009-10-31 13:38

>>15

Dragon book is good, except it's a bit disorganized.

Ex/

This is how you write a grammar.  You don't want it to be left recursive. We'll show you how to avoid that 5 chapters later.

As long as you don't mind jumping around a LOT, it's great.

Name: Anonymous 2009-10-31 18:34

Please refrain from posting non-programming related material.

Thank you.

Name: Anonymous 2009-10-31 19:13

>>26
Fuckpants

Name: Anonymous 2009-10-31 19:32

Pantfucks.

Name: Anonymous 2009-10-31 19:48

>>26

/*  Universal Compiler
    Compiles any source language to any target architecture or language.
    Copyright (C)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/

/* Main include header for the Universal Compiler library */

#ifndef _UC_H
#define _UC_H

int compile(const char *langSrc, const char *langDest, const char* sFile);

#endif


Remember to link against libunivc.a

Don't change these.
Name: Email:
Entire Thread Thread List