示例#1
0
文件: mecssy.c 项目: theory/mecssy
int parse_css(FILE *fh) {
    // Set up the scanner
    yyscan_t scanner;
    yylex_init(&scanner);
    yyset_in(fh, scanner);

    // Set up the parser
    void *css_parser = ParseAlloc(malloc);
    int lexCode;

    do {
        lexCode = yylex(scanner);
        printf("Got %i\n", lexCode);
        Parse(css_parser, lexCode, yyget_text(scanner));
    } while (lexCode > 0);

    // Finish the job.
    Parse(css_parser, 0, NULL);

    if (-1 == lexCode) {
        fprintf(stderr, "The scanner encountered an error.\n");
        return 0;
    }

    // Cleanup the scanner and parser
    yylex_destroy(scanner);
    ParseFree(css_parser, free);

    return 1;

}
示例#2
0
ModuleNode *getAST(const char *expr)
{
    ModuleNode *expression;
    yyscan_t scanner;
    YY_BUFFER_STATE state;

    if (yylex_init(&scanner)) {
        // couldn't initialize
        return NULL;
    }

    state = yy_scan_string(expr, scanner);

    yydebug = 0;
    if (yyparse(&expression, scanner)) {
        // error parsing
        return NULL;
    }

    yy_delete_buffer(state, scanner);

    yylex_destroy(scanner);

    return expression;
}
示例#3
0
文件: calc.cpp 项目: pbmartins/LFA
int main(int argc, char *argv[])
{
    /* instantiate and init calculator data */
    CalcData calc_data;

    if (yylex_init(&calc_data.scaninfo))
    {
        fprintf(stderr, "Fail initing scanner\n");
        return(EXIT_FAILURE);
    }

    calc_data.symt = new SymbolTable;

    /* set up input stream */
    yyset_in(stdin, calc_data.scaninfo);

    /* call the parser */
    printf("> "); fflush(stdout);
    int ret;
    while ((ret=yyparse(&calc_data)) != 0)
    {
        printf("  = %g\n", calc_data.ast->eval());
        printf("> "); fflush(stdout);
    }
    printf("\n");

    /* clean up and quit */
    yylex_destroy(calc_data.scaninfo);
    delete calc_data.ast;
    delete calc_data.symt;
    return 0;
}
示例#4
0
lexer::lexer (char const *name)
  : impl (new pimpl (name))
{
  yylex_init (&yyscanner);
  yyset_extra (this, yyscanner);
  yyset_in (NULL, yyscanner);
}
示例#5
0
ltl_formula *
getAST (const char *input)
{
  ltl_formula *formula;
  yyscan_t scanner;
  YY_BUFFER_STATE state;

  if (yylex_init (&scanner))
    {
      // couldn't initialize
      return NULL;
    }

  state = yy_scan_string (input, scanner);
  if (yyparse (&formula, scanner))
    {
      // error parsing
      return NULL;
    }

  yy_delete_buffer (state, scanner);
  yylex_destroy (scanner);

  return formula;
}
示例#6
0
jsgf_t *
jsgf_parse_string(const char *string, jsgf_t * parent)
{
    yyscan_t yyscanner;
    jsgf_t *jsgf;
    int yyrv;
    YY_BUFFER_STATE buf;

    yylex_init(&yyscanner);
    buf = yy_scan_string(string, yyscanner);

    jsgf = jsgf_grammar_new(parent);
    if (!parent)
        jsgf_set_search_path(jsgf, NULL);

    yyrv = yyparse(yyscanner, jsgf);
    if (yyrv != 0) {
        E_ERROR("Failed to parse JSGF grammar from input string\n");
        jsgf_grammar_free(jsgf);
        yy_delete_buffer(buf, yyscanner);
        yylex_destroy(yyscanner);
        return NULL;
    }
    yy_delete_buffer(buf, yyscanner);
    yylex_destroy(yyscanner);

    return jsgf;
}
示例#7
0
vcx_ast_module*
vcx_parser_parse_file(const char* filename)
{
    vcx_parse_ctx ctx;
    int r;
    FILE* stream;

    stream = fopen(filename, "r");
    if (stream == NULL) {
        vcx_util_errorf("could not open source file: %s", filename);
        return NULL;
    }

    yylex_init(&ctx.scanner);
    yyset_extra(&ctx, ctx.scanner);
    yyset_in(stream, ctx.scanner);
    r = yyparse(&ctx);
    yylex_destroy(ctx.scanner);
    fclose(stream);

    if (r == 0) {
        return ctx.root;
    }
    else {
        vcx_ast_module_delete(ctx.root);
        return NULL;
    }
}
示例#8
0
int main( int argc, const char *argv[] ) {
    void *scanner;

    FILE *input;
    if( argc >= 2 ) input = fopen( argv[1], "r" );
    else            input = stdin;

    if( input == NULL ) {
        perror( "cannot open input file" );
        exit( 1 );
    }

    if( argc >= 3 )  {
        mkparent( argv[2] );
        FILE *res = freopen( argv[2], "w", stdout );
        if( res == NULL ) {
            perror( "cannot open output file" );
            exit( 1 );
        }
    }

    yylex_init( &scanner );
    yyset_in( input, scanner );
    yyparse( scanner );
    yylex_destroy( scanner );

    exit(0);
}
示例#9
0
文件: Main.c 项目: Winter-M/Trot
pt_node *buildAST(const char *expr) {
	extern int yyparse(pt_node **, yyscan_t);

	FILE *file;
	yyscan_t scanner;
	pt_node *node = NULL;
	YY_BUFFER_STATE state;

	if(yylex_init(&scanner)) {
		fprintf(stderr, "Failed to initialize parser.\n");
		exit(-1);
	}

	file = fopen(expr, "r");
	if(file == NULL) {
		perror("fopen");
		exit(-1);
	}

	state = yy_create_buffer(file, 1024, scanner);
	yy_switch_to_buffer(state, scanner);

	if(yyparse(&node, scanner)) {
		fprintf(stderr, "Error parsing input.\n");
		exit(-1);
	}

	yy_delete_buffer(state, scanner);
	yylex_destroy(scanner);

	return node;
}
示例#10
0
void fsqlf_format_bytes(fsqlf_kwmap_t kwmap,
    const char *bytes_in, int len, char **bytes_out
)
{
    struct fsqlf_formatter_state f_state;
    FSQLF_stack_init(&f_state.lexstate_stack, sizeof(int));
    FSQLF_stack_init(&f_state.sub_openings, sizeof(pair));
    FSQLF_tokque_init(&f_state.tqueue);
    f_state.currindent = 0;
    f_state.left_p = 0;
    f_state.right_p = 0;
    f_state.kwall = kwmap;
    f_state.bout.len_used = 0;
    f_state.bout.len_alloc = len * 1.5;
    f_state.bout.buffer = malloc(f_state.bout.len_alloc);

    yyscan_t scanner;
    yylex_init(&scanner);
    yy_scan_bytes(bytes_in, len, scanner);
    yyset_extra(&f_state, scanner);

    yylex(scanner);
    *bytes_out = f_state.bout.buffer;

    yylex_destroy(scanner);
}
示例#11
0
range* do_range_expand(range_request* rr, const char* text)
{
    yyscan_t scanner;
    struct range_extras extra;
    int result;

    if (text == NULL) {
        range* r = range_new(rr);
        range_request_set(rr, r);
        return r;
    }
    current_rr = rr;
    extra.rr = rr;

    yylex_init(&scanner);
    yyset_extra(&extra, scanner);
    yy_scan_string(text, scanner);
    result = yyparse(scanner);
    yylex_destroy(scanner);
    current_rr = NULL;

    if (result != 0) {
        range* r = range_new(rr);
        range_request_warn(rr, "parsing [%s]", text);
        range_request_set(rr, r);
        return r;
    }

    range_request_set(rr, range_evaluate(rr, extra.theast));
    return range_request_results(rr);
}
示例#12
0
bool InteractiveParser::parse(FILE *file, const std::string *filename, parser::ProgramNode &programStmts, stmtFunction callback) const {
    yydebug = DEBUG_PARSER;

    yyscan_t scanner;
    yylex_init(&scanner);

    bool interactive = (stdin == file);
#ifndef OS_WINDOWS
    interactive = interactive && (isatty(fileno(file)) == 1);
#endif
    yyset_debug(DEBUG_LEXER, scanner);

    LexerContext lexerCtx(&programStmts, filename, interactive, callback);
    yylex_init_extra(&lexerCtx, &scanner);
    lexerCtx.scanner = scanner;
    lexerCtx.interactive = interactive;
#ifndef OS_WINDOWS
    if(stdin == file){
        rl_instream = stdin;
    }
#endif
    if(interactive){
        std::cout << "Welcome to the sugar interactive shell" << std::endl;
        std::cout << "Use 'quit' or 'exit' to quit this shell" << std::endl;
    }

    yy_push_state(lex_state_newline, scanner);
    yyset_in( file, scanner );
    bool succeed = (yyparse(&lexerCtx) == 0);

    yylex_destroy(scanner);
    fclose(file);
    return succeed && (interactive || !lexerCtx.hasError);
}
示例#13
0
jsgf_t *
jsgf_parse_file(const char *filename, jsgf_t *parent)
{
    yyscan_t yyscanner;
    jsgf_t *jsgf;
    int yyrv;
    FILE *in = NULL;

    yylex_init(&yyscanner);
    if (filename == NULL) {
        yyset_in(stdin, yyscanner);
    }
    else {
        in = fopen(filename, "r");
        if (in == NULL) {
            E_ERROR_SYSTEM("Failed to open %s for parsing", filename);
            return NULL;
        }
        yyset_in(in, yyscanner);
    }

    jsgf = jsgf_grammar_new(parent);
    yyrv = yyparse(yyscanner, jsgf);
    if (yyrv != 0) {
        E_ERROR("Failed to parse JSGF grammar from '%s'\n", filename ? filename : "(stdin)");
        jsgf_grammar_free(jsgf);
        yylex_destroy(yyscanner);
        return NULL;
    }
    if (in)
        fclose(in);
    yylex_destroy(yyscanner);

    return jsgf;
}
示例#14
0
int como_ast_create(const char *filename)
{
    ast_node* statements;
    yyscan_t scanner;
    YY_BUFFER_STATE state;
    char* text;

    text = file_get_contents(filename);

    if(!text) {
        printf("file '%s' not found\n", filename);
        return 1;
    }
    
    if(yylex_init(&scanner)) {
        como_error_noreturn("yylex_init returned NULL");
    }

    state = yy_scan_string(text, scanner);

    if(yyparse(&statements, scanner)) {
        como_error_noreturn("yyparse returned NULL");
    }

    yy_delete_buffer(state, scanner);

    yylex_destroy(scanner);

    como_compile_ast(statements, filename);

    return 0;
}
示例#15
0
文件: val.c 项目: blynn/symple
void val_parse_forall(char *s, val_callback_t callback) {
  yyscan_t scanner;
  if (yylex_init(&scanner)) die("yylex_init error");
  YY_BUFFER_STATE buf = s ? yy_scan_string(s, scanner) : 0;
  if (yyparse(scanner, callback)) die("parse error");
  yy_delete_buffer(buf, scanner);
  yylex_destroy(scanner);
}
示例#16
0
文件: kcalc.c 项目: vbmacher/lecalc
/**
 * Initialize parser.
 */
void initialize(FILE *input_file, FILE *output_file) {
  if (mode_of_operation == MODE_INTERACTIVE) {
    fprintf(output_file, PACKAGE_STRING "\n(c) Copyright 2010-2011, P.Jakubco <" 
           PACKAGE_BUGREPORT ">\n\nStarting interactive mode.\n(Type 'help' for help.)\n");
  }

  yylex_init(&yyscanner);
  yyset_in(input_file, yyscanner);
  yyset_out(output_file, yyscanner);
}
示例#17
0
/******************************************************************************
 * Public Functions
 *****************************************************************************/
void Parse_ParseInput(HashTable_T* symbols, FILE* in_file)
{
	if ( in_file != NULL )
	{
		ParseContext_T context = { NULL, symbols};
		context.symbols = symbols;
		yylex_init( &(context.lexinfo) );
		yyset_in( in_file, context.lexinfo );
		yyparse( &context );
	}
}
示例#18
0
parse_context *
parse_context_new (void)
{
  parse_context *self = mem_alloc (sizeof *self);

  yylex_init (&self->scanner);
  yyset_extra (self, self->scanner);

  self->unit = NULL;

  return self;
}
示例#19
0
文件: compiler.cpp 项目: Raekye/hmmm
ASTNode* Compiler::parse(std::string code) {
	ASTNode* root;
	yyscan_t scanner;
	YY_BUFFER_STATE buffer;
	if (yylex_init(&scanner)) {
		return NULL;
	}
	buffer = yy_scan_string(code.c_str(), scanner);
	yyparse(&root, scanner);
	yy_delete_buffer(buffer, scanner);
	yylex_destroy(scanner);
	return root;
}
示例#20
0
static PyObject * _psp_module_parsestring(PyObject *self, PyObject *argv)
{
    PyObject *code;
    PyObject *str;
    PyObject *latin = NULL;
    char *c_str = NULL;
    yyscan_t scanner;
    psp_parser_t  *parser;
    YY_BUFFER_STATE bs;

    if (!PyArg_ParseTuple(argv, "S", &str)) {
        return NULL;
    }

    Py_BEGIN_ALLOW_THREADS
    parser = psp_parser_init();
    yylex_init(&scanner);
    yyset_extra(parser, scanner);

    if (PyUnicode_Check(str)) {
        latin = PyUnicode_AsLatin1String(str);
        if (latin)
            c_str = PyBytes_AsString(latin);
    } else if (PyBytes_Check(str))
        c_str = PyBytes_AsString(str);

    if (!c_str) c_str = "UNICODE ERROR";

    bs = yy_scan_string(c_str, scanner);
    yylex(scanner);

    Py_XDECREF(latin);

    /* yy_delete_buffer(bs, scanner); */
    yylex_destroy(scanner);

    psp_string_0(&parser->pycode);
    Py_END_ALLOW_THREADS

    if (parser->pycode.blob) {
        code = MpBytesOrUnicode_FromString(parser->pycode.blob);
    }
    else {
        code = MpBytesOrUnicode_FromString("");
    }

    psp_parser_cleanup(parser);

    return code;
}
示例#21
0
文件: compiler.c 项目: proty/proty
int Compiler_compileFile(Context* context, FILE* file) {
    void* scanner;
    Node* root = 0;

    yylex_init(&scanner);
    yyset_in(file, scanner);

    int res = yyparse(scanner, &root);
    yylex_destroy(scanner);
    if (res) return -1;

    res = Compiler_compileRootNode(context, root);
    if (root) Node_delete(root);
    return res;
}
示例#22
0
文件: compiler.c 项目: proty/proty
int Compiler_compileString(Context* context, const char* str) {
    void* scanner;
    Node* root = 0;

    yylex_init(&scanner);
    yy_scan_string(str, scanner);

    int res = yyparse(scanner, &root);
    yylex_destroy(scanner);
    if (res) return -1;

    res = Compiler_compileRootNode(context, root);
    if (root) Node_delete(root);
    return res;
}
示例#23
0
int parser_parse(FILE* in, FILE* out) {
    int result = 0;
    yyscan_t scanner;
    if (yylex_init(&scanner)) {
        result = 1;
    }
    else {
        yyset_in(in, scanner);
        yyset_out(out, scanner);
    
        result = yyparse(scanner, yyparse_callback);
        yylex_destroy(scanner);
    }

    return (result);
}
示例#24
0
文件: main.c 项目: crab2313/m1
int
main(int argc, char *argv[]) {
    FILE        *fp;
    yyscan_t     yyscanner;
    M1_compiler  comp;
    
    if (argc <= 1) {
        fprintf(stderr, "Usage: m1 <file>\n");
        exit(EXIT_FAILURE);    
    }
    
    fp = fopen(argv[1], "r");
    if (fp == NULL) {
        fprintf(stderr, "Could not open file\n");
        exit(EXIT_FAILURE);
    }
   
    /* set up compiler */
    init_compiler(&comp);
                                       
    /* set up lexer and parser */   	
    yylex_init(&yyscanner);    
    yyset_extra(&comp, yyscanner); 
    yyset_in(fp, yyscanner);
    
    comp.yyscanner = yyscanner; /* yyscanner has a pointer to comp, and vice versa. */
    
    yyparse(yyscanner, &comp);
    
    fprintf(stderr, "parsing done\n");
    if (comp.errors == 0) 
    {
        assert(intstack_isempty(comp.breakstack) != 0);
        assert(intstack_isempty(comp.continuestack) != 0);
        
    	check(&comp, comp.ast); /*  need to finish */
    	//if (comp.errors == 0) 
    	{
        	fprintf(stderr, "generating code...\n");
	        gencode(&comp, comp.ast);
    	}
    }
    
    fclose(fp);
    fprintf(stderr, "compilation done\n");
    return 0;
}
示例#25
0
unsigned char parse_and_report(const char *filename)
{
	yyscan_t scanner;
	FILE *f;
	unsigned char err = 0;

	if(filename == NULL || strcmp(filename, "-") == 0) {
		f = stdin;
		filename = "stdin";
	} else {
		f = fopen(filename, "r");
		if(!f) {
			perror(filename);
			return 1;
		}
	}

	yylex_init(&scanner);
	yyset_in(f, scanner);
	yyset_extra((void *) filename, scanner);
#ifdef DEBUG
	yyset_debug(1, scanner);
#endif

	if(xml_output) printf("\t<file name=\"%s\">\n", filename);

	switch(yyparse(scanner)) {
	case 0:
		if(!xml_output) printf("%s: valid JSON\n", filename);
		break;
	case 1:
		if(!xml_output) printf("%s: parsing failed\n", filename);
		err = 1;
		break;
	case 2:
		fprintf(stderr, "Out of memory\n");
		exit(1);
		break;
	}

	if(xml_output) printf("\t</file>\n");

	yylex_destroy(scanner);
	if(f != stdin) fclose(f);

	return err;
}
示例#26
0
int LSCPServer::Main() {
    hSocket = socket(AF_INET, SOCK_STREAM, 0);
    if (hSocket < 0) {
        std::cerr << "LSCPServer: Could not create server socket." << std::endl;
        return -1;
    }

    SocketAddress.sin_family      = AF_INET;
    SocketAddress.sin_port        = htons(LSCP_PORT);
    SocketAddress.sin_addr.s_addr = htonl(INADDR_ANY);

    if (bind(hSocket, (sockaddr*) &SocketAddress, sizeof(sockaddr_in)) < 0) {
        std::cerr << "LSCPServer: Could not bind server socket." << std::endl;
        close(hSocket);
        return -1;
    }

    listen(hSocket, 1);
    dmsg(1,("LSCPServer: Server running.\n")); // server running

    // now wait for client connections and handle their requests
    sockaddr_in client;
    int length = sizeof(client);
    while (true) {
        hSession = accept(hSocket, (sockaddr*) &client, (socklen_t*) &length);
        if (hSession < 0) {
            std::cerr << "LSCPServer: Client connection failed." << std::endl;
            close(hSocket);
            return -1;
        }

        dmsg(1,("LSCPServer: Client connection established.\n"));
        //send(hSession, "Welcome!\r\n", 10, 0);

        // Parser invocation
        yyparse_param_t yyparse_param;
        yyparse_param.pServer = this;
        yylex_init(&yyparse_param.pScanner);
        while (yyparse(&yyparse_param) == LSCP_SYNTAX_ERROR); // recall parser in case of syntax error
        yylex_destroy(yyparse_param.pScanner);

        close(hSession);
        dmsg(1,("LSCPServer: Client connection terminated.\n"));
    }
}
示例#27
0
struct parser_state *cli_js_init(void)
{
	struct parser_state *state = cli_calloc(1, sizeof(*state));
	if(!state)
		return NULL;
	if(!scope_new(state)) {
		free(state);
		return NULL;
	}
	state->global = state->current;

	if(yylex_init(&state->scanner)) {
		scope_done(state->global);
		free(state);
		return NULL;
	}
	cli_dbgmsg(MODULE "cli_js_init() done\n");
	return state;
}
示例#28
0
文件: main.cpp 项目: Raekye/hmmm
NExpression* getAST(const char* str) {
	NExpression* expr;
	yyscan_t scanner;
	YY_BUFFER_STATE state;
	if (yylex_init(&scanner)) {
		return NULL;
	}

	state = yy_scan_string(str, scanner);

	if (yyparse(&expr, scanner)) {
		return NULL;
	}

	yy_delete_buffer(state, scanner);
	yylex_destroy(scanner);

	return expr;
}
示例#29
0
const node& generator_context::set_script(const std::string& name,
                                   const std::string& script)
{
    function* func;
    yyscan_t scanner;
    YY_BUFFER_STATE state;

    if (yylex_init(&scanner))
        throw std::runtime_error("yylex_init failed");

    state = yy_scan_string(script.c_str(), scanner);
    if (yyparse(&func, scanner))
        throw std::runtime_error("yyparse failed");

    yy_delete_buffer(state, scanner);
    yylex_destroy(scanner);

    auto result = scripts_.emplace(std::make_pair(name, node(func, *this)));
    delete func;
    
    return result.first->second;
}
示例#30
0
void fsqlf_format_file(struct fsqlf_kw_conf *kwall, FILE *fin, FILE *fout)
{
    struct fsqlf_formatter_state f_state;
    FSQLF_stack_init(&f_state.lexstate_stack, sizeof(int));
    FSQLF_stack_init(&f_state.sub_openings, sizeof(pair));
    FSQLF_tokque_init(&f_state.tqueue);
    f_state.currindent = 0;
    f_state.left_p = 0;
    f_state.right_p = 0;
    f_state.kwall = kwall;
    f_state.bout = (struct FSQLF_out_buffer){NULL, 0, 0};

    yyscan_t scanner;
    yylex_init(&scanner);
    yyset_in(fin, scanner);
    yyset_out(fout, scanner);
    yyset_extra(&f_state, scanner);

    yylex(scanner);

    yylex_destroy(scanner);
}