BinPAC Compiler
From BroWiki
BinPAC compiler
Contents |
Intro
Scanning and Parsing
Scanning and parsing is done by flex and bison generated code. A user can learn the formal grammar specification from aux/binpac/src/pac_scan.ll and aux/binpac/src/pac_parse.yy under the Bro source tree.
Scanning
Scanner has four start conditions:
- EC - Scanning embedded code
Input is appended into EmbeddedCode untill %}. Also parse for embedded_pac_primitive
- PP - Scanning PAC primitive
- INCL - Scanning include
- RE - Scanning a regex
Parsing
As binpac parses through input, it builds a declaration class for each construct. There are six different types of declarations:
- EnumDecl
- FuncDecl
- HelperDecl - Embbeded C code
- LetDecl
- RegExDecl - Regex defintion
- TypeDecl - analyzer, connection, flow, record definition
The generated declaration is added to decl_list_ static DeclList *decl_list_; // pac_decl.h
Code generation
void Decl::ProcessDecls(Output *out_h, Output *out_cc)
This function is the entry point to code generation. It goes through four phases: preparation, generate extern declaration, generate forward declaration, and generate code. It iterates through all declaration by the order which they occur in the PAC file.
Preparation
Before code is generated, the compiler iterate through all the declaration and prepare them for code generation. Typical action are adding Param and Fields.
HelperDecl
Do nothing
TypeDecl
RegExDecl
Add regex ID into global env as GLOBAL_VAR.
Generate Extern Declaration
Generate Forward Declaration
Generate Code
Class reference
Object is the base class which all other language elements extend.
- filename
- line_num
- location
AnalyzerElement
Used to store fields in flow and connection
AnalyzerFlow
upflow/downflow information, used in connection.
AnalyzerHelper
Store embedded code in connection and flow
ID
Contains the name and the location of an ID. Typically used to store declaration name.
- name
- anonymous_id
- locname
Decl
TypeDecl
- Type *type_
AnalyzerContextDecl
- current_analyzer_context_ - global context pointer, initialized to 0
- constructor - create new context and set current_analyzer_context_
AnalyzerDecl
ConnDecl
Connection declaration
Type
- UNDEF
- EMPTY
- BUILTIN
- PARAMETERIZED
- RECORD
- CASE
- ARRAY
- STRING
- EXTERN
- DUMMY

