-
大小: 52KB文件類型: .rar金幣: 2下載: 1 次發布日期: 2021-06-25
- 語言: C/C++
- 標簽: Lex_Yacc??編譯原理??課程設計??cygwin_C++??
資源簡介
作者: 胡彥
本框架是一個lex/yacc完整的示例,用于學習lex/yacc程序基本的搭建方法,在linux/cygwin下敲入make就可以編譯和執行。
本例子雖小卻演示了lex/yacc程序最常見和重要的特征:
* lex/yacc文件格式、程序結構。
* 如何在lex/yacc中使用C++和STL庫,用extern "C"聲明那些lex/yacc生成的、要鏈接的C函數,如yylex(), yywrap(), yyerror()。
* 重定義YYSTYPE/yylval為復雜類型。
* 用%token方式聲明yacc記號。
* 用%type方式聲明非終結符的類型。
* lex里正則表達式的定義、識別方式。
* lex里用yylval向yacc返回屬性值。
* 在yacc嵌入的C代碼動作里,對記號屬性($1, $2等)、和非終結符屬性($$)的正確引用方法。
* 對yyin/yyout重賦值,以改變yacc默認的輸入/輸出目標。
* 如何開始解析(yyparse函數),結束或繼續解析(yywrap函數)。
本例子功能是,對當前目錄下的file.txt文件,解析出其中的標識符、數字、其它符號,顯示在屏幕上。linux調試環境是Ubuntu 10.04。
總之,大部分框架已經搭好了,你只要稍加擴展就可以成為一個計算器之類的程序,用于《編譯原理》的課程設計。
文件列表:
lex.l: lex程序文件。
yacc.y: yacc程序文件。
main.hpp: 共同使用的頭文件。
Makefile: makefile文件。
file.txt: 給程序解析的文本文件。
使用方法:
1-把lex_yacc_example.rar解壓到linux/cygwin下。
2-命令行進入lex_yacc_example目錄。
3-敲入make,這時會自動執行以下操作:
(1) 自動調用flex編譯.l文件,生成lex.yy.c文件。
(2) 自動調用bison編譯.y文件,生成yacc.tab.c和yacc.tab.h文件。
(3) 自動調用g++編譯、鏈接出可執行文件main。
(4) 自動執行main,得到如下結果:。
bison -d yacc.y
g++ -c lex.yy.c
g++ -c yacc.tab.c
g++ lex.yy.o yacc.tab.o -o main
id: abc
id: defghi
int: 123
int: 45678
op: !
op: @
op: #
op: $
AllId: abc defghi
參考資料:《Lex和Yacc從入門到精通(6)-解析C-C++包含文件》, http://blog.csdn.net/pandaxcl/article/details/1321552
其它文章和代碼請留意我的blog: http://blog.csdn.net/huyansoft
2013-4-27

代碼片段和文件信息
#line?3?“lex.yy.c“
#define??YY_INT_ALIGNED?short?int
/*?A?lexical?scanner?generated?by?flex?*/
#define?FLEX_SCANNER
#define?YY_FLEX_MAJOR_VERSION?2
#define?YY_FLEX_MINOR_VERSION?5
#define?YY_FLEX_SUBMINOR_VERSION?35
#if?YY_FLEX_SUBMINOR_VERSION?>?0
#define?FLEX_BETA
#endif
/*?First?we?deal?with??platform-specific?or?compiler-specific?issues.?*/
/*?begin?standard?C?headers.?*/
#include?
#include?
#include?
#include?
/*?end?standard?C?headers.?*/
/*?flex?integer?type?definitions?*/
#ifndef?FLEXINT_H
#define?FLEXINT_H
/*?C99?systems?have?.?Non-C99?systems?may?or?may?not.?*/
#if?defined?(__STDC_VERSION__)?&&?__STDC_VERSION__?>=?199901L
/*?C99?says?to?define?__STDC_LIMIT_MACROS?before?including?stdint.h
?*?if?you?want?the?limit?(max/min)?macros?for?int?types.?
?*/
#ifndef?__STDC_LIMIT_MACROS
#define?__STDC_LIMIT_MACROS?1
#endif
#include?
typedef?int8_t?flex_int8_t;
typedef?uint8_t?flex_uint8_t;
typedef?int16_t?flex_int16_t;
typedef?uint16_t?flex_uint16_t;
typedef?int32_t?flex_int32_t;
typedef?uint32_t?flex_uint32_t;
#else
typedef?signed?char?flex_int8_t;
typedef?short?int?flex_int16_t;
typedef?int?flex_int32_t;
typedef?unsigned?char?flex_uint8_t;?
typedef?unsigned?short?int?flex_uint16_t;
typedef?unsigned?int?flex_uint32_t;
/*?Limits?of?integral?types.?*/
#ifndef?INT8_MIN
#define?INT8_MIN???????????????(-128)
#endif
#ifndef?INT16_MIN
#define?INT16_MIN??????????????(-32767-1)
#endif
#ifndef?INT32_MIN
#define?INT32_MIN??????????????(-2147483647-1)
#endif
#ifndef?INT8_MAX
#define?INT8_MAX???????????????(127)
#endif
#ifndef?INT16_MAX
#define?INT16_MAX??????????????(32767)
#endif
#ifndef?INT32_MAX
#define?INT32_MAX??????????????(2147483647)
#endif
#ifndef?UINT8_MAX
#define?UINT8_MAX??????????????(255U)
#endif
#ifndef?UINT16_MAX
#define?UINT16_MAX?????????????(65535U)
#endif
#ifndef?UINT32_MAX
#define?UINT32_MAX?????????????(4294967295U)
#endif
#endif?/*?!?C99?*/
#endif?/*?!?FLEXINT_H?*/
#ifdef?__cplusplus
/*?The?“const“?storage-class-modifier?is?valid.?*/
#define?YY_USE_CONST
#else /*?!?__cplusplus?*/
/*?C99?requires?__STDC__?to?be?defined?as?1.?*/
#if?defined?(__STDC__)
#define?YY_USE_CONST
#endif /*?defined?(__STDC__)?*/
#endif /*?!?__cplusplus?*/
#ifdef?YY_USE_CONST
#define?yyconst?const
#else
#define?yyconst
#endif
/*?Returned?upon?end-of-file.?*/
#define?YY_NULL?0
/*?Promotes?a?possibly?negative?possibly?signed?char?to?an?unsigned
?*?integer?for?use?as?an?array?index.??If?the?signed?char?is?negative
?*?we?want?to?instead?treat?it?as?an?8-bit?unsigned?char?hence?the
?*?double?cast.
?*/
#define?YY_SC_TO_UI(c)?((unsigned?int)?(unsigned?char)?c)
/*?Enter?a?start?condition.??This?macro?really?ought?to?take?a?parameter
?*?but?we?do?it?the?disgusting?crufty?way?forced?on?us?by?the?()-less
?*?definition?of?BEGIN.
?*/
#define?BEGIN?(yy_start)?=?1?+?2?*
/*?Translate?the?current?start?state?into?a?value?that?can?be?later?handed
?*?to?BEGIN?to?return?to?the?state.??The?
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
????.......??????2022??2013-04-27?17:09??lex_yacc_example\yacc.tab.h
????.......?????42722??2013-04-27?17:09??lex_yacc_example\yacc.tab.c
?????文件????????606??2013-04-27?15:21??lex_yacc_example\main.hpp
?????文件?????????32??2013-04-27?15:56??lex_yacc_example\file.txt
?????文件???????1973??2013-04-27?17:00??lex_yacc_example\lex.l
?????文件???????1783??2013-04-27?17:32??lex_yacc_example\README.txt
?????文件??????35430??2013-04-27?17:09??lex_yacc_example\main
????.......?????27024??2013-04-27?17:09??lex_yacc_example\lex.yy.o
????.......?????46235??2013-04-27?17:07??lex_yacc_example\lex.yy.c
?????文件???????3118??2013-04-27?17:09??lex_yacc_example\yacc.y
?????文件????????320??2013-04-27?15:25??lex_yacc_example\Makefile
????.......?????12464??2013-04-27?17:09??lex_yacc_example\yacc.tab.o
?????目錄??????????0??2013-04-27?17:32??lex_yacc_example
-----------?---------??----------?-----??----
???????????????173729????????????????????13
- 上一篇:c++builder2010 授權文件
- 下一篇:回溯算法實現5皇后問題
評論
共有 條評論