Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
B
bpftrace
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
bpftrace
Commits
e31923da
Commit
e31923da
authored
Dec 15, 2016
by
Alastair Robertson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add variables and assignment to grammar
parent
76333562
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
61 additions
and
19 deletions
+61
-19
src/ast.cpp
src/ast.cpp
+24
-11
src/ast.h
src/ast.h
+19
-5
src/lexer.l
src/lexer.l
+1
-0
src/main.cpp
src/main.cpp
+3
-1
src/parser.yy
src/parser.yy
+14
-2
No files found.
src/ast.cpp
View file @
e31923da
...
...
@@ -6,36 +6,49 @@ namespace ast {
void
Integer
::
print_ast
(
std
::
ostream
&
out
,
unsigned
int
depth
)
const
{
out
<<
"int: "
<<
n
<<
std
::
endl
;
std
::
string
indent
(
depth
,
' '
);
out
<<
indent
<<
"int: "
<<
n
<<
std
::
endl
;
}
void
Identifier
::
print_ast
(
std
::
ostream
&
out
,
unsigned
int
depth
)
const
void
Variable
::
print_ast
(
std
::
ostream
&
out
,
unsigned
int
depth
)
const
{
out
<<
"ident: "
<<
ident
<<
std
::
endl
;
std
::
string
indent
(
depth
,
' '
);
out
<<
indent
<<
"var: "
<<
ident
<<
std
::
endl
;
if
(
vargs
!=
nullptr
)
{
for
(
Expression
*
expr
:
*
vargs
)
{
expr
->
print_ast
(
out
,
depth
+
1
);
}
}
}
void
ExprStatement
::
print_ast
(
std
::
ostream
&
out
,
unsigned
int
depth
)
const
{
std
::
string
indent
(
depth
,
' '
);
expr
->
print_ast
(
out
,
depth
);
}
void
Statement
::
print_ast
(
std
::
ostream
&
out
,
unsigned
int
depth
)
const
void
Assign
Statement
::
print_ast
(
std
::
ostream
&
out
,
unsigned
int
depth
)
const
{
out
<<
"stmt"
<<
std
::
endl
;
out
<<
std
::
string
(
depth
,
' '
);
std
::
string
indent
(
depth
,
' '
);
out
<<
indent
<<
"="
<<
std
::
endl
;
var
->
print_ast
(
out
,
depth
+
1
);
expr
->
print_ast
(
out
,
depth
+
1
);
}
void
Probe
::
print_ast
(
std
::
ostream
&
out
,
unsigned
int
depth
)
const
{
out
<<
"Probe"
<<
std
::
endl
;
std
::
string
indent
(
depth
,
' '
);
out
<<
indent
<<
"Probe"
<<
std
::
endl
;
for
(
Statement
*
stmt
:
*
stmts
)
{
out
<<
std
::
string
(
depth
,
' '
);
stmt
->
print_ast
(
out
,
depth
+
1
);
}
}
void
Program
::
print_ast
(
std
::
ostream
&
out
,
unsigned
int
depth
)
const
{
out
<<
"Program"
<<
std
::
endl
;
++
depth
;
std
::
string
indent
(
depth
,
' '
)
;
out
<<
indent
<<
"Program"
<<
std
::
endl
;
for
(
Probe
*
probe
:
*
probes
)
{
out
<<
std
::
string
(
depth
,
' '
);
probe
->
print_ast
(
out
,
depth
+
1
);
}
}
...
...
src/ast.h
View file @
e31923da
...
...
@@ -16,6 +16,7 @@ public:
class
Expression
:
public
Node
{
};
using
ExpressionList
=
std
::
vector
<
Expression
*>
;
class
Integer
:
public
Expression
{
public:
...
...
@@ -24,24 +25,37 @@ public:
int
n
;
};
class
Identifier
:
public
Expression
{
class
Variable
:
public
Expression
{
public:
explicit
Identifier
(
std
::
string
&
ident
)
:
ident
(
ident
)
{
}
explicit
Variable
(
std
::
string
&
ident
)
:
ident
(
ident
),
vargs
(
nullptr
)
{
}
Variable
(
std
::
string
&
ident
,
ExpressionList
*
vargs
)
:
ident
(
ident
),
vargs
(
vargs
)
{
}
void
print_ast
(
std
::
ostream
&
out
,
unsigned
int
depth
=
0
)
const
override
;
std
::
string
ident
;
ExpressionList
*
vargs
;
};
class
Statement
:
public
Node
{
};
using
StatementList
=
std
::
vector
<
Statement
*>
;
class
ExprStatement
:
public
Statement
{
public:
explicit
Statement
(
Expression
*
expr
)
:
expr
(
expr
)
{
}
explicit
Expr
Statement
(
Expression
*
expr
)
:
expr
(
expr
)
{
}
void
print_ast
(
std
::
ostream
&
out
,
unsigned
int
depth
=
0
)
const
override
;
Expression
*
expr
;
};
using
StatementList
=
std
::
vector
<
Statement
*>
;
class
AssignStatement
:
public
Statement
{
public:
AssignStatement
(
Variable
*
var
,
Expression
*
expr
)
:
var
(
var
),
expr
(
expr
)
{
}
void
print_ast
(
std
::
ostream
&
out
,
unsigned
int
depth
=
0
)
const
override
;
Variable
*
var
;
Expression
*
expr
;
};
class
Probe
:
public
Node
{
public:
Probe
(
std
::
string
type
,
std
::
string
attach_point
,
StatementList
*
stmts
)
Probe
(
std
::
string
&
type
,
std
::
string
&
attach_point
,
StatementList
*
stmts
)
:
type
(
type
),
attach_point
(
attach_point
),
stmts
(
stmts
)
{
}
void
print_ast
(
std
::
ostream
&
out
,
unsigned
int
depth
=
0
)
const
override
;
...
...
src/lexer.l
View file @
e31923da
...
...
@@ -32,6 +32,7 @@ using namespace ebpf::bpftrace;
";" { return Parser::make_SEMI(loc); }
"{" { return Parser::make_LBRACE(loc); }
"}" { return Parser::make_RBRACE(loc); }
"=" { return Parser::make_ASSIGN(loc); }
. { driver.error(loc, std::string("invalid character: ")+std::string(yytext)); yyterminate(); }
...
...
src/main.cpp
View file @
e31923da
...
...
@@ -12,6 +12,8 @@ int main(int argc, char *argv[])
result
=
driver
.
parse
(
argv
[
1
]);
}
driver
.
root_
->
print_ast
(
std
::
cout
);
if
(
!
result
)
driver
.
root_
->
print_ast
(
std
::
cout
);
return
result
;
}
src/parser.yy
View file @
e31923da
...
...
@@ -42,6 +42,7 @@ void yyerror(ebpf::bpftrace::Driver &driver, const char *s);
SEMI ";"
LBRACE "{"
RBRACE "}"
ASSIGN "="
;
%token <std::string> IDENT "identifier"
...
...
@@ -52,6 +53,8 @@ void yyerror(ebpf::bpftrace::Driver &driver, const char *s);
%type <ast::Probe *> probe
%type <ast::Statement *> stmt
%type <ast::Expression *> expr
%type <ast::Variable *> var
%type <ast::ExpressionList *> vargs
%printer { yyoutput << %%; } <*>;
...
...
@@ -76,13 +79,22 @@ stmts : stmts ";" stmt { $$ = $1; $1->push_back($3); }
| stmt { $$ = new ast::StatementList; $$->push_back($1); }
;
stmt : expr { $$ = new ast::Statement($1); }
stmt : expr { $$ = new ast::ExprStatement($1); }
| var "=" expr { $$ = new ast::AssignStatement($1, $3); }
;
expr : INT { $$ = new ast::Integer($1); }
|
IDENT { $$ = new ast::Identifier($1)
; }
|
var { $$ = $1
; }
;
var : IDENT { $$ = new ast::Variable($1); }
| IDENT "[" vargs "]" { $$ = new ast::Variable($1, $3); }
;
vargs : vargs "," expr { $$ = $1; $1->push_back($3); }
| expr { $$ = new ast::ExpressionList; $$->push_back($1); }
;
%%
void ebpf::bpftrace::Parser::error(const location &l, const std::string &m)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment