Docsity

Sml-Yacc Parser Generator - Compiler Design - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Sml-Yacc Parser Generator - Compiler Design - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Lecture #11, Feb. 19, 2007

  • ml-Yacc
  • Actions when reducing
  • Making ml-yacc work with ml-lex
  • Boiler plate

Sml-yacc parser generator

  • Sml-yacc specifications contain 3 parts

separated by %%

%term and %nonterm

  • These things look like algebraic datatype declarations.
  • We will build an example parser for Regular Expressions.

( user declarations ) datatype Re = empty of int | simple of string * int | concat of Re * Re | closure of Re | union of Re * Re; val count = ref 0; fun next() =(count := (!count)+1; !count);

%% ( declarations about the grammar ) %name XXX

%term EOF | STAR | BAR | LP | RP | HASH | SINGLE of string

%nonterm exp of Re %pos int

%% ( grammar rules )

... Docsity.com

Description of Example

• Symbols are represented by EOF, BAR, ...

• None of them have any attributes except SINGLE

which has a string attribute which represents the

single character that we want to recognize.

• There is only one non-terminal, exp, and it has one

attribute which is of type Re.

• Note that the Re type is defined in the user

declarations section.

• The %pos declaration say that a position is an

integer. This is for error reporting.

Grammar Rules Section

  • Grammar rules which describe the grammar that is to be
recognized
  • They also tell what to do whenever a "reduce" action is
encountered.
  • A grammar rule has the form:
    • <non-terminal> : ( action )
    • For example: exp: SINGLE ( simple(SINGLE,next() ) ) | HASH ( empty (next()) )
  • The “action” is a value that is associated with the lhs of the
production when it is pushed on the stack.
  • Its can “depend” upon the values of the symbols in the rhs (which
are already on the stack).

(user declarations (Re) omitted here)Example Showing Grammar Rules

%%

(* declarations about the grammar *)

%name XXX

%term EOF | STAR | BAR | LP |

RP | HASH | SINGLE of string

%nonterm exp of Re

%pos int

%%

exp: SINGLE ( simple(SINGLE ,next()) )

| HASH ( empty(next()) ) | LP exp RP ( exp ) | exp STAR ( closure exp ) | exp exp ( concat(exp1,exp2) ) | exp BAR exp ( union(exp1,exp2) )

Complete Example continued %%

go: exp EOF ( exp ) exp: SINGLE ( simple(SINGLE,next()) ) | HASH ( empty( next() ) ) | LP exp RP ( exp ) | exp STAR ( closure exp ) | exp exp %prec DUMMY ( concat(exp1,exp2) ) | exp BAR exp ( union(exp1,exp2) )

Boiler Plate

  • To get this all to work we need a lexical analyzer that can produce terminal symbols with the correct attributes for the %term directive.
  • We can use sml-lex to do this, but instead of defining our own token type we will use the one which is automatically defined by the %term declaration in sml-yacc.
  • In order to do this we need the following BOILER-PLATE in the user declarations part of the sml-lex source file.

type pos = int type svalue = Tokens.svalue type ('a,'b) token = ('a,'b) Tokens.token type lexresult = (svalue,pos) token open Tokens val lineno = ref 0 val reset_lineno = fn () => lineno := 1 val eof = fn () => EOF(!lineno,!lineno) fun error (e,l : int,_) =...

Boiler plate in Sml-Lex source file

Tying it all together

• The file "XXX.cm" ties all the pieces together.

• This file has many occurrences of the string XXX,

they must all be changed to the same string as in

the %name directive of the sml-yacc source file.

• To build a parser we do the following:

  • Start up sml and then use the compile-manager as follows

New Boiler plate for Parser

structure CommonTypes = struct ( Put type declarations here that you ) ( want to appear in both the parser ) ( and lexer. You can open this structure ) ( else where inside your application as well ) end;

group is CommonTypes.sml XXX.lex XXX.grm driver.sml

( Other user defined sml files go here )

$/basis.cm ( system library files ) $/smlnj-lib.cm $/ml-yacc-lib.cm

XXX.cm

CommonTypes.sml

The .lex files open CommonTypes;

type pos = int type svalue = Tokens.svalue

(* the type token is from the %term in XXX.grm *) type ('a,'b) token = ('a,'b) Tokens.token type lexresult = (svalue,pos) token

(* Defines constructor functions for "token" *) open Tokens val lineno = ref 0 val reset_lineno = fn () => lineno := 1

... (* YOUR USER DECLARATIONS (if any) GO HERE *)

%% %header (functor XXXLexFun(structure Tokens:XXX_TOKENS));

(* YOUR Lex-Definitions (if any) GO HERE *)

%%

(* YOUR RULES GO HERE *)

XXX.lex

The .grm file open CommonTypes;

(* YOUR USER DECLARATIONS (if any) GO HERE *)

%% (* declarations about the grammar *) %name XXX

%term EOF | ...

%nonterm go of? | ...

%pos int %start go %eop EOF %verbose

(* YOUR GRAMMAR DECLARATIONS LIKE %left ETC. (if any) GO HERE *)

%%

go: ... EOF ( ... )

(* YOUR ADDITINAL GRAMMAR RULES GO HERE *)

XXX.grm

Standard ML of New Jersey v110.57 [built: Mon Nov 21 21:46:28 2005]

  • CM.make "regexp.cm"; [scanning regexp.cm] [D:\programs\SML110.57\bin\ml-lex regexp.lex]

Number of states = 12 Number of distinct rows = 2 Approx. memory size of trans. table = 258 bytes [parsing (regexp.cm):regexp.lex.sml] [library $/ml-yacc-lib.cm is stable] [library $SMLNJ-ML-YACC-LIB/ml-yacc-lib.cm is stable] [loading (regexp.cm):regexp.grm.sig] [loading (regexp.cm):CommonTypes.sml] [loading (regexp.cm):regexp.grm.sml] [compiling (regexp.cm):regexp.lex.sml] [code: 9617, data: 705, env: 1871 bytes] [loading (regexp.cm):driver.sml] [New bindings added.] val it = true : bool

  • open Driver; opening Driver val parse : string -> bool -> Driver.regexpParser.result val verboselex : bool ref end