EnglishFrenchGermanSpainItalianDutchPortugueseRussianKoreanJapaneseArabic Chinese Simplified

Monday, June 20, 2011

OOP Recursive Decent Parsing I

First off we need to define what our parser is going to do; we need to a construct a set of terminals and non-terminals to work with, in other words a “grammar.” Let’s look at the following grammar.

expr -> term ‘+’ term
expr -> term ‘-‘ term
exor -> term
term -> unary ‘*’ unary
term -> unary ‘/’ unary
term -> unary
unary -> ‘-‘ factor
unary -> ‘+’ factor
unary -> factor
factor -> '(' expr ')'
factor -> number

In this case a factor is either a number or an expression enclosed by parenthesis, a factor could be a symbol or string literal, as well. For the purpose of this tutorial we will just use numbers. Also we are using a fairly limited set of operators

Let’s get down to business. First we want to set things up a bit; we want to predefine our classes and create some building blocks.
#ifndef PARSER_H_INCLUDED
#define PARSER_H_INCLUDED

#include <istream>
#include <deque>
#include <exception>

//pre-declare are classes so they can be used in a non-discriminate order
class Production;
class Expr;
class Term;
class Unary;
class Number;
class Factor;
class ParseError;

/**
* the base class for all of our productions
*/
class Production {
public:
 virtual ~Production(); //dose nothing, if we don't need to override it we don't want to
 virtual double getValue()=0; //pure virtual, all children MUST implmnet this function
};

/**
* a simple class to handle parsing errors
*/
class ParseError : public std::exception {
 const char* what() const throw();
};

Artikel Terkait:

Comments :

2 comments to “OOP Recursive Decent Parsing I”

waniaeastling said...
on 

2021 escape titanium - Titanium Art
Experience titanium dive watch a world of luxury with the best escape titanium on the world micro touch titanium trim of Tienen Art columbia titanium pants and the beauty of our beautiful art 2014 ford fusion energi titanium work. head titanium ti s6

Unknown said...
on 

t748n4qtiny504 prostate massagers,cheap sex toys,horse dildo,sex toys,adult sex toys,real dolls,sex toys,horse dildo,women sex toys b987p4fdehr468

Post a Comment

 

Copyright © 2009 by Learn Technology

Template by Blogger Templates | Powered by Blogger