venkysmarty
venkysmarty

Reputation: 11431

pipes and filter architecture

I am reading about pipes and filter architecture pattern in Pattern oriented software architecture. Here I don't have to know compiler design, but author gave example with compiler design. I followed most of it, but I have difficulty in understanding following.

As in compiler design we have different phases like scanner, parser, semantic analysis, intermediate code generation, and backends (MIPS backend, Intel backend...).

Here author mentioned front end stages as , parser, semantic analysis, intermediate code generation. and back end is MIPS backend.

Following is text snippet

We decide not to construct an abstract syntax tree explicitly, to be passed from the parser to the semantic analyzer. Instead we embed calls to the semantic analyzer(sa) and code generator(cg) into yacc's grammer rules:

addexpr  : team
         | addexpr '+' term
           { sa.checkCompat($1,$3); cg.genAdd($1,$3);}
         | addexpr '-' term
           { sa.checkCompat($1,$3); cg.genSub($1,$3);}

My questions on above text 1. What does author mean by " not to construct abstract tree explictily" ? 2. I just need to understand above grammer rules what does it doing? As I am not designing lanuguage, I have to understand pattern. If I have good understanding on above example I can follow pattern effectively?

Thanks for your time and help.

Upvotes: 0

Views: 473

Answers (3)

Sameer
Sameer

Reputation: 4389

I think Morten Jensen answered question about the interpretation of grammar. In addition, I am guessing :

  • sa.checkCompat is referring to - Semantic Analyzer, Check Compatibility of the two operands
  • cg.genAdd is referring to - Code Generator, Generate code to Add the two operands

The main thing author is pointing out is that the calls to the "next" stages of compilation, SA and CG are invoked within the Parsing stage. In other words, it does not wait for the entire Parser tree to be generated "before" doing Semantic Analysis and Code Generation.

Upvotes: 1

mikalai
mikalai

Reputation: 1736

I believe @MortenJensen answered your first questions.

If you made some web development, probably you already know what pipeline is. Just imagine your request processing at a web server. There is a line of independent modules which parse their input (which is output of previous module in a line) and the last one generates output http response. (tcp package transformed to http request, which transformed to server objects, request is parsed and response server objects created, objects rendered to html, html wrapped into http response, which is wrapped into tcp package).

The same is about compiler. It gets your source code and after several consequent transformations you'll get an executable. It's pretty good sample for pipeline pattern, but probably not with such details which can distract you from the main problem.

Upvotes: 1

Morten Jensen
Morten Jensen

Reputation: 5936

This example looks like grammar rules from a lexical parser like yacc-lex/bison-flex. A rule like the one you are describing, is a sort of hierarchical grammar rule such as BNF.

The 'code' you are posting tells the grammar rule of a type of expression called "addexpr".

addexpr := term | addexpr + term | addexpr - term

This reads something like:

An add-expression is a term or another add-expression + a term or another add-expression - a term.

You can use these recursive grammar rules to build up a language presentation. The code { sa.checkCompat($1,$3); cg.genAdd($1,$3);} looks like it's doing an input-validation on $1 and $3 - as in argument 1 and 3 where in "X + Y" $1 would be 'X', $2='+' and $3='Y'. The call to cg.genAdd is probably a function call that adds an 'add'-expression to your abstract syntax tree.

That means you're generating the abstract syntax tree one element at a time, while you're parsing your source code.

Upvotes: 0

Related Questions