abstract-syntax-tree

Attributes from Boost.Spirit grammar: error from std:vector of boost::variant

陌路散爱 提交于 2019-12-18 08:48:23
问题 I got a working parser for reading position descriptions for a board game (international draughts, official grammar): #include <boost/spirit/home/x3.hpp> #include <iostream> namespace x3 = boost::spirit::x3; auto const colon = x3::lit(':'); auto const comma = x3::lit(','); auto const dash = x3::lit('-'); auto const dot = x3::lit('.'); auto const king = x3::char_('K'); auto const color = x3::char_("BW"); auto const num_sq = x3::int_; auto const num_pc = -king >> num_sq; // Kxx means king on

Boost::spirit::qi defining a calculator for nullaries

我的梦境 提交于 2019-12-18 08:41:06
问题 I'm trying to write a parser for math expressions where named variables are nullaries in boost::spirit (version 1_51_0), to which I'm completely new. I define typedef boost::function<double()> Value and my rules will be declared like so: qi::rule<Iterator, Value()> expression, term, others, ...; I define binary operators on nullaries with this macro #define BINARY_FUNCTOR(name, op) \ struct name \ { \ name(Value x, Value y): x_(x), y_(y) {} \ double operator()() { return x_() op y_(); } \

Boost::spirit::qi defining a calculator for nullaries

夙愿已清 提交于 2019-12-18 08:40:03
问题 I'm trying to write a parser for math expressions where named variables are nullaries in boost::spirit (version 1_51_0), to which I'm completely new. I define typedef boost::function<double()> Value and my rules will be declared like so: qi::rule<Iterator, Value()> expression, term, others, ...; I define binary operators on nullaries with this macro #define BINARY_FUNCTOR(name, op) \ struct name \ { \ name(Value x, Value y): x_(x), y_(y) {} \ double operator()() { return x_() op y_(); } \

How to automatically insert pragmas in your program

我与影子孤独终老i 提交于 2019-12-18 08:39:15
问题 I need to write a tool that can take a C code and put pragmas on top of some functions. Which compiler framework is the easiest to do such a task. Also if you can provide an example, I would really appreciate it. 回答1: If you want to do this reliably, you need a full C front end, and the ability to modify parsed code. Our DMS Software Reengineering Toolkit with its C Front End can probably do what you want. DMS can parse, build ASTs, and carry out custom transformations on source text, either

Why should json.loads be preferred to ast.literal_eval for parsing JSON?

跟風遠走 提交于 2019-12-18 04:48:06
问题 I have a dictionary that is stored in a db field as a string. I am trying to parse it into a dict, but json.loads gives me an error. Why does json.loads fail on this and ast.literal_eval works? Is one preferable over the other? >>> c.iframe_data u"{u'person': u'Annabelle!', u'csrfmiddlewaretoken': u'wTE9RZGvjCh9RCL00pLloxOYZItQ98JN'}" # json fails >>> json.loads(c.iframe_data) Traceback (most recent call last): ValueError: Expecting property name enclosed in double quotes: line 1 column 2

Build AST from C code

一笑奈何 提交于 2019-12-18 04:09:17
问题 How can I build an AST (Abstract Syntax Tree) from gcc C code in order to make some modifications, like converting some int variables to float, and reproduce(generate) the code to C syntax again after that. Actually, for the moment, the only functionality I truly need is to extract a table of variables and their types from a c program consisting of few lines... I think there is a simple parser doing so. I have some variables like: int var_bss ; float var_f_bss; int var_data = 4; float var_f

TypeScript: get syntax tree

只愿长相守 提交于 2019-12-17 23:29:39
问题 I had read "whole internet", but can't find any examples about getting syntax tree (just like in Esprima) from TypeScrypt source. I mean how can i get object like this (Esprima Parser example) { "type": "Program", "body": [ { "type": "VariableDeclaration", "declarations": [ { "type": "VariableDeclarator", "id": { "type": "Identifier", "name": "answer" }, "init": { "type": "BinaryExpression", "operator": "*", "left": { "type": "Literal", "value": 6, "raw": "6" }, "right": { "type": "Literal",

How to work with AST with Cofree annotation?

拈花ヽ惹草 提交于 2019-12-17 23:15:46
问题 I have this simple Expr AST and I can easily convert it to String . import Prelude hiding (Foldable) import qualified Prelude import Data.Foldable as F import Data.Functor.Foldable import Data.Monoid import Control.Comonad.Cofree data ExprF r = Const Int | Add r r deriving ( Show, Eq, Ord, Functor, Prelude.Foldable ) type Expr = Fix ExprF testExpr = Fix $ Add (Fix (Const 1)) (Fix (Const 2)) convertToString :: Expr -> String convertToString = cata $ \case e@(Const x) -> show x e@(Add x y) ->

Restricting Python's syntax to execute user code safely. Is this a safe approach?

喜欢而已 提交于 2019-12-17 22:53:15
问题 Original question: Executing mathematical user code on a python web server, what is the simplest secure way? I want to be able to run user submitted code on a python webserver. The code will be simple and mathematical in nature. As such a small subset of Python is required, my current approach is to whitelist allowable syntax by traversing Python's abstract syntax tree. Functions and names get special treatment; only explicitly whitelisted functions are allowed, and only unused names. import

Can I get an XML AST dump of C/C++ code with clang without using the compiler?

社会主义新天地 提交于 2019-12-17 22:46:48
问题 I managed to compile successfully clang for windows with cmake and visual studio 10. I would like to get an XML file as AST representation of the source code. There is one option that provides the result with clang with gcc under linux (ubuntu) but doesn't work on the windows box: clang -cc1 -ast-print-xml source.c However, this is invoking the compilation stage (which I would like to avoid). Digging in the source code didn't help me so far as I am quite new to clang. I could manage to