-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.h
executable file
·125 lines (107 loc) · 4 KB
/
Parser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#ifndef PARSER_H
#define PARSER_H
/////////////////////////////////////////////////////////////////////
// Parser.h - Analyzes C++ language constructs //
// ver 1.2 //
// Language: Visual C++ 2008, SP1 //
// Platform: Dell Precision T7400, Vista Ultimate SP1 //
// Application: Prototype for CSE687 Pr1, Sp09 //
// Author: Jim Fawcett, CST 4-187, Syracuse University //
// (315) 443-3948, jfawcett@twcny.rr.com //
/////////////////////////////////////////////////////////////////////
/*
Module Operations:
==================
This module defines a Parser class. Its instances collect
semi-expressions from a file for analysis. Analysis consists of
applying a set of rules to the semi-expression, and for each rule
that matches, a set of actions is invoked.
Public Interface:
=================
Toker t(someFile); // create tokenizer instance
SemiExp se(&t); // create a SemiExp attached to tokenizer
Parser parser(se); // now we have a parser
Rule1 r1; // create instance of a derived Rule class
Action1 a1; // create a derived action
r1.addAction(&a1); // register action with the rule
parser.addRule(&r1); // register rule with parser
while(se.getSemiExp()) // get semi-expression
parser.parse(); // and parse it
Build Process:
==============
Required files
- Parser.h, Parser.cpp, SemiExpression.h, SemiExpression.cpp,
tokenizer.h, tokenizer.cpp,
ActionsAndRules.h, ActionsAndRules.cpp,
ConfigureParser.h, ConfigureParser.cpp
Build commands (either one)
- devenv Parser.sln
- cl /EHsc /DTEST_PARSER parser.cpp semiexpression.cpp tokenizer.cpp \
ActionsAndRules.cpp ConfigureParser.cpp /link setargv.obj
Maintenance History:
====================
ver 1.2 : 17 Jan 09
- modified to accept pointer to interface ITokCollection instead of
SemiExpression
ver 1.1 : 17 Sep 07
- cosmetic modifications to comments on this page
ver 1.0 : 12 Jan 06
- first release
*/
#include <string>
#include <iostream>
#include <vector>
#include "ITokCollection.h"
///////////////////////////////////////////////////////////////
// abstract base class for parsing actions
// - when a rule succeeds, it invokes any registered action
class Parser;
class IBuilder
{
public:
virtual ~IBuilder() {}
virtual Parser* Build()=0;
};
///////////////////////////////////////////////////////////////
// abstract base class for parsing actions
// - when a rule succeeds, it invokes any registered action
class IAction
{
public:
virtual ~IAction() {}
virtual void doAction(ITokCollection* pTc)=0;
};
///////////////////////////////////////////////////////////////
// abstract base class for parser language construct detections
// - rules are registered with the parser for use
class IRule
{
public:
virtual ~IRule() {}
void addAction(IAction* pAction);
void doActions(ITokCollection* pTc);
virtual bool doTest(ITokCollection* pTc)=0;
protected:
std::vector<IAction*> actions;
};
///////////////////////////////////////////////////////////////
// class that implements a parser
// - It collects semi-expressions from a file for analysis.
// - Each rule is applied to each semi-expression. if the
// rule matches, then it invokes any registered actions.
class Parser
{
public:
Parser(ITokCollection* pTokCollection);
~Parser();
void addRule(IRule* pRule);
bool parse();
bool next();
private:
ITokCollection* pTokColl;
std::vector<IRule*> rules;
};
inline Parser::Parser(ITokCollection* pTokCollection) : pTokColl(pTokCollection) {}
inline Parser::~Parser() {}
inline bool Parser::next() { return pTokColl->get(); }
#endif