├── .idea ├── .name ├── .gitignore ├── yacc_bison_practice.iml ├── codeStyles │ └── codeStyleConfig.xml ├── vcs.xml ├── misc.xml └── modules.xml ├── cmake-build-debug ├── CMakeFiles │ ├── progress.marks │ ├── clion-environment.txt │ ├── test.dir │ │ ├── link.txt │ │ ├── progress.make │ │ ├── test.c.o │ │ ├── depend.make │ │ ├── flags.make │ │ ├── depend.internal │ │ ├── cmake_clean.cmake │ │ ├── C.includecache │ │ ├── DependInfo.cmake │ │ └── build.make │ ├── cmake.check_cache │ ├── 3.16.5 │ │ ├── CompilerIdC │ │ │ └── a.out │ │ ├── CompilerIdCXX │ │ │ └── a.out │ │ ├── CMakeDetermineCompilerABI_C.bin │ │ ├── CMakeDetermineCompilerABI_CXX.bin │ │ ├── CMakeSystem.cmake │ │ ├── CMakeCCompiler.cmake │ │ └── CMakeCXXCompiler.cmake │ ├── clion-log.txt │ ├── CMakeDirectoryInformation.cmake │ ├── TargetDirectories.txt │ └── Makefile.cmake ├── ch2 │ ├── 3.txt │ ├── CMakeFiles │ │ ├── progress.marks │ │ ├── 2.03_read_files.dir │ │ │ ├── progress.make │ │ │ ├── link.txt │ │ │ ├── depend.make │ │ │ ├── flags.make │ │ │ ├── depend.internal │ │ │ ├── cmake_clean.cmake │ │ │ ├── C.includecache │ │ │ ├── DependInfo.cmake │ │ │ └── build.make │ │ ├── 2.01_usual_lang_lex_re.dir │ │ │ ├── progress.make │ │ │ ├── link.txt │ │ │ ├── depend.make │ │ │ ├── flags.make │ │ │ ├── depend.internal │ │ │ ├── cmake_clean.cmake │ │ │ ├── C.includecache │ │ │ ├── DependInfo.cmake │ │ │ └── build.make │ │ ├── 2.06_include_file_and_start_state.dir │ │ │ ├── progress.make │ │ │ ├── link.txt │ │ │ ├── flags.make │ │ │ ├── depend.make │ │ │ ├── depend.internal │ │ │ ├── cmake_clean.cmake │ │ │ ├── C.includecache │ │ │ └── DependInfo.cmake │ │ ├── 2.02_use_yyin_to_read_data_from_file.dir │ │ │ ├── progress.make │ │ │ ├── link.txt │ │ │ ├── flags.make │ │ │ ├── depend.make │ │ │ ├── depend.internal │ │ │ ├── cmake_clean.cmake │ │ │ ├── C.includecache │ │ │ └── DependInfo.cmake │ │ └── CMakeDirectoryInformation.cmake │ ├── 2.txt │ ├── 1.txt │ └── cmake_install.cmake ├── ch3 │ ├── CMakeFiles │ │ ├── progress.marks │ │ ├── 3.05.dir │ │ │ ├── progress.make │ │ │ ├── link.txt │ │ │ ├── depend.make │ │ │ ├── flags.make │ │ │ ├── depend.internal │ │ │ ├── cmake_clean.cmake │ │ │ ├── C.includecache │ │ │ ├── DependInfo.cmake │ │ │ └── build.make │ │ └── CMakeDirectoryInformation.cmake │ ├── 3.02 │ │ ├── CMakeFiles │ │ │ ├── progress.marks │ │ │ ├── 3.02_create_AST_with_bison.dir │ │ │ │ ├── progress.make │ │ │ │ ├── link.txt │ │ │ │ ├── flags.make │ │ │ │ ├── cmake_clean.cmake │ │ │ │ ├── depend.make │ │ │ │ ├── depend.internal │ │ │ │ ├── C.includecache │ │ │ │ └── DependInfo.cmake │ │ │ └── CMakeDirectoryInformation.cmake │ │ ├── cmake_install.cmake │ │ └── 3.02_create_AST_with_bison.tab.h │ ├── 3.05 │ │ ├── CMakeFiles │ │ │ ├── progress.marks │ │ │ ├── 3.05_bak.dir │ │ │ │ ├── depend.make │ │ │ │ ├── progress.make │ │ │ │ ├── link.txt │ │ │ │ ├── flags.make │ │ │ │ ├── cmake_clean.cmake │ │ │ │ └── DependInfo.cmake │ │ │ ├── 3.05.dir │ │ │ │ ├── progress.make │ │ │ │ ├── link.txt │ │ │ │ ├── flags.make │ │ │ │ ├── cmake_clean.cmake │ │ │ │ ├── DependInfo.cmake │ │ │ │ ├── C.includecache │ │ │ │ ├── depend.make │ │ │ │ ├── depend.internal │ │ │ │ └── CXX.includecache │ │ │ └── CMakeDirectoryInformation.cmake │ │ ├── cmake_install.cmake │ │ └── stack.hh │ ├── cmake_install.cmake │ └── Makefile ├── ch5 │ ├── CMakeFiles │ │ ├── progress.marks │ │ └── CMakeDirectoryInformation.cmake │ ├── cmake_install.cmake │ └── Makefile ├── ch1 │ ├── 1.06 │ │ ├── CMakeFiles │ │ │ ├── progress.marks │ │ │ ├── 1.06.dir │ │ │ │ ├── progress.make │ │ │ │ ├── link.txt │ │ │ │ ├── cmake_clean.cmake │ │ │ │ ├── flags.make │ │ │ │ ├── depend.make │ │ │ │ ├── depend.internal │ │ │ │ ├── C.includecache │ │ │ │ └── DependInfo.cmake │ │ │ └── CMakeDirectoryInformation.cmake │ │ ├── cmake_install.cmake │ │ └── 1.06_calculator_with_bison.tab.h │ ├── CMakeFiles │ │ ├── progress.marks │ │ ├── 1.01_word_counter.dir │ │ │ ├── progress.make │ │ │ ├── link.txt │ │ │ ├── depend.make │ │ │ ├── depend.internal │ │ │ ├── C.includecache │ │ │ ├── flags.make │ │ │ ├── cmake_clean.cmake │ │ │ ├── DependInfo.cmake │ │ │ └── build.make │ │ ├── 1.04_NextToken_with_Flex.dir │ │ │ ├── progress.make │ │ │ ├── link.txt │ │ │ ├── depend.internal │ │ │ ├── depend.make │ │ │ ├── flags.make │ │ │ ├── cmake_clean.cmake │ │ │ ├── C.includecache │ │ │ ├── DependInfo.cmake │ │ │ └── build.make │ │ ├── 1.02_BritishEn_to_AmericanEn.dir │ │ │ ├── progress.make │ │ │ ├── depend.make │ │ │ ├── link.txt │ │ │ ├── C.includecache │ │ │ ├── depend.internal │ │ │ ├── flags.make │ │ │ ├── cmake_clean.cmake │ │ │ ├── DependInfo.cmake │ │ │ └── build.make │ │ ├── 1.03_recognize_calculator_tokens.dir │ │ │ ├── progress.make │ │ │ ├── depend.make │ │ │ ├── link.txt │ │ │ ├── C.includecache │ │ │ ├── depend.internal │ │ │ ├── flags.make │ │ │ ├── cmake_clean.cmake │ │ │ └── DependInfo.cmake │ │ └── CMakeDirectoryInformation.cmake │ └── cmake_install.cmake ├── test └── cmake_install.cmake ├── ch3 ├── CMakeLists.txt ├── 3.05 │ ├── main.cpp │ ├── driver.cpp │ ├── driver.h │ ├── CMakeLists.txt │ ├── scanner.h │ ├── lexer.l │ └── parser.y └── 3.02 │ ├── 3.02lexer.l │ ├── fb3.02.h │ ├── CMakeLists.txt │ ├── 3.02_create_AST_with_bison.y │ └── fb3.02.c ├── README.md ├── test.l ├── ch1 ├── 1.02_BritishEn_to_AmericanEn.l ├── README.md ├── 1.03_recognize_calculator_tokens.l ├── 1.06 │ ├── 1.06lexer.l │ ├── CMakeLists.txt │ └── 1.06_calculator_with_bison.y ├── 1.05_first_calculator_with_bison.y ├── 1.01_word_counter.l ├── CMakeLists.txt ├── 1.04_main.c └── 1.04_NextToken_with_Flex.l ├── cmake └── clean_flex_bison_generate_code.cmake ├── ch2 ├── CMakeLists.txt ├── 2.02_use_yyin_to_read_data_from_file.l ├── 2.03_read_files.l ├── 2.01_usual_lang_lex_re.l └── 2.06_include_file_and_start_state.l └── CMakeLists.txt /.idea/.name: -------------------------------------------------------------------------------- 1 | Flex_Bison -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/progress.marks: -------------------------------------------------------------------------------- 1 | 28 2 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/3.txt: -------------------------------------------------------------------------------- 1 | 2 | xx 3 | yy 4 | zz 5 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/progress.marks: -------------------------------------------------------------------------------- 1 | 8 2 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/CMakeFiles/progress.marks: -------------------------------------------------------------------------------- 1 | 9 2 | -------------------------------------------------------------------------------- /cmake-build-debug/ch5/CMakeFiles/progress.marks: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /workspace.xml -------------------------------------------------------------------------------- /cmake-build-debug/ch1/1.06/CMakeFiles/progress.marks: -------------------------------------------------------------------------------- 1 | 3 2 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/progress.marks: -------------------------------------------------------------------------------- 1 | 11 2 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/2.txt: -------------------------------------------------------------------------------- 1 | 2 | aa 3 | bb 4 | cc 5 | 6 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.02/CMakeFiles/progress.marks: -------------------------------------------------------------------------------- 1 | 4 2 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.05/CMakeFiles/progress.marks: -------------------------------------------------------------------------------- 1 | 5 2 | -------------------------------------------------------------------------------- /ch3/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | add_subdirectory(3.02) 4 | add_subdirectory(3.05) 5 | -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/clion-environment.txt: -------------------------------------------------------------------------------- 1 | ToolSet: 1.0 (local)Options: 2 | 3 | Options: -------------------------------------------------------------------------------- /cmake-build-debug/ch2/1.txt: -------------------------------------------------------------------------------- 1 | 2 | #include <2.txt> 3 | #include "3.txt" 4 | 5 | 12 6 | 34 7 | 56 8 | -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/test.dir/link.txt: -------------------------------------------------------------------------------- 1 | /usr/bin/cc -g CMakeFiles/test.dir/test.c.o -o test 2 | -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/test.dir/progress.make: -------------------------------------------------------------------------------- 1 | CMAKE_PROGRESS_1 = 1 2 | CMAKE_PROGRESS_2 = 2 3 | 4 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/CMakeFiles/3.05.dir/progress.make: -------------------------------------------------------------------------------- 1 | CMAKE_PROGRESS_1 = 1 2 | CMAKE_PROGRESS_2 = 2 3 | 4 | -------------------------------------------------------------------------------- /cmake-build-debug/test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ronnie88597/yacc_bison_practice/HEAD/cmake-build-debug/test -------------------------------------------------------------------------------- /cmake-build-debug/ch3/CMakeFiles/3.05.dir/link.txt: -------------------------------------------------------------------------------- 1 | /usr/bin/cc -g CMakeFiles/3.05.dir/__/3.05lexer.c.o -o 3.05 2 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.01_word_counter.dir/progress.make: -------------------------------------------------------------------------------- 1 | CMAKE_PROGRESS_1 = 1 2 | CMAKE_PROGRESS_2 = 2 3 | 4 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.03_read_files.dir/progress.make: -------------------------------------------------------------------------------- 1 | CMAKE_PROGRESS_1 = 16 2 | CMAKE_PROGRESS_2 = 17 3 | 4 | -------------------------------------------------------------------------------- /.idea/yacc_bison_practice.iml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/progress.make: -------------------------------------------------------------------------------- 1 | CMAKE_PROGRESS_1 = 7 2 | CMAKE_PROGRESS_2 = 8 3 | 4 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/progress.make: -------------------------------------------------------------------------------- 1 | CMAKE_PROGRESS_1 = 12 2 | CMAKE_PROGRESS_2 = 13 3 | 4 | -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/cmake.check_cache: -------------------------------------------------------------------------------- 1 | # This file is generated by cmake for dependency checking of the CMakeCache.txt file 2 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/progress.make: -------------------------------------------------------------------------------- 1 | CMAKE_PROGRESS_1 = 3 2 | CMAKE_PROGRESS_2 = 4 3 | 4 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.03_recognize_calculator_tokens.dir/progress.make: -------------------------------------------------------------------------------- 1 | CMAKE_PROGRESS_1 = 5 2 | CMAKE_PROGRESS_2 = 6 3 | 4 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/1.06/CMakeFiles/1.06.dir/progress.make: -------------------------------------------------------------------------------- 1 | CMAKE_PROGRESS_1 = 9 2 | CMAKE_PROGRESS_2 = 10 3 | CMAKE_PROGRESS_3 = 11 4 | 5 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.06_include_file_and_start_state.dir/progress.make: -------------------------------------------------------------------------------- 1 | CMAKE_PROGRESS_1 = 18 2 | CMAKE_PROGRESS_2 = 19 3 | 4 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.02_use_yyin_to_read_data_from_file.dir/progress.make: -------------------------------------------------------------------------------- 1 | CMAKE_PROGRESS_1 = 14 2 | CMAKE_PROGRESS_2 = 15 3 | 4 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.03_read_files.dir/link.txt: -------------------------------------------------------------------------------- 1 | /usr/bin/cc -g CMakeFiles/2.03_read_files.dir/2.03_read_files.c.o -o 2.03_read_files 2 | -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/test.dir/test.c.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ronnie88597/yacc_bison_practice/HEAD/cmake-build-debug/CMakeFiles/test.dir/test.c.o -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.05/CMakeFiles/3.05_bak.dir/depend.make: -------------------------------------------------------------------------------- 1 | # Empty dependencies file for 3.05_bak. 2 | # This may be replaced when dependencies are built. 3 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.01_word_counter.dir/link.txt: -------------------------------------------------------------------------------- 1 | /usr/bin/cc -g CMakeFiles/1.01_word_counter.dir/1.01_word_counter.c.o -o 1.01_word_counter -lfl 2 | -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/3.16.5/CompilerIdC/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ronnie88597/yacc_bison_practice/HEAD/cmake-build-debug/CMakeFiles/3.16.5/CompilerIdC/a.out -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/3.16.5/CompilerIdCXX/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ronnie88597/yacc_bison_practice/HEAD/cmake-build-debug/CMakeFiles/3.16.5/CompilerIdCXX/a.out -------------------------------------------------------------------------------- /cmake-build-debug/ch1/1.06/CMakeFiles/1.06.dir/link.txt: -------------------------------------------------------------------------------- 1 | /usr/bin/cc -g CMakeFiles/1.06.dir/1.06lexer.c.o CMakeFiles/1.06.dir/1.06_calculator_with_bison.tab.c.o -o 1.06 -lfl 2 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.01_word_counter.dir/depend.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | 5 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/link.txt: -------------------------------------------------------------------------------- 1 | /usr/bin/cc -g CMakeFiles/1.04_NextToken_with_Flex.dir/1.04_main.c.o -o 1.04_NextToken_with_Flex -lfl 2 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/link.txt: -------------------------------------------------------------------------------- 1 | /usr/bin/cc -g CMakeFiles/2.01_usual_lang_lex_re.dir/2.01_usual_lang_lex_re.c.o -o 2.01_usual_lang_lex_re -lfl 2 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.02/CMakeFiles/3.02_create_AST_with_bison.dir/progress.make: -------------------------------------------------------------------------------- 1 | CMAKE_PROGRESS_1 = 20 2 | CMAKE_PROGRESS_2 = 21 3 | CMAKE_PROGRESS_3 = 22 4 | CMAKE_PROGRESS_4 = 23 5 | 6 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/depend.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | 5 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.03_recognize_calculator_tokens.dir/depend.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | 5 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.05/CMakeFiles/3.05.dir/progress.make: -------------------------------------------------------------------------------- 1 | CMAKE_PROGRESS_1 = 24 2 | CMAKE_PROGRESS_2 = 25 3 | CMAKE_PROGRESS_3 = 26 4 | CMAKE_PROGRESS_4 = 27 5 | CMAKE_PROGRESS_5 = 28 6 | 7 | -------------------------------------------------------------------------------- /.idea/codeStyles/codeStyleConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/3.16.5/CMakeDetermineCompilerABI_C.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ronnie88597/yacc_bison_practice/HEAD/cmake-build-debug/CMakeFiles/3.16.5/CMakeDetermineCompilerABI_C.bin -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.05/CMakeFiles/3.05_bak.dir/progress.make: -------------------------------------------------------------------------------- 1 | CMAKE_PROGRESS_1 = 24 2 | CMAKE_PROGRESS_2 = 25 3 | CMAKE_PROGRESS_3 = 26 4 | CMAKE_PROGRESS_4 = 27 5 | CMAKE_PROGRESS_5 = 28 6 | 7 | -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/3.16.5/CMakeDetermineCompilerABI_CXX.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ronnie88597/yacc_bison_practice/HEAD/cmake-build-debug/CMakeFiles/3.16.5/CMakeDetermineCompilerABI_CXX.bin -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/test.dir/depend.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | CMakeFiles/test.dir/test.c.o: test.c 5 | 6 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/link.txt: -------------------------------------------------------------------------------- 1 | /usr/bin/cc -g CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/1.02_BritishEn_to_AmericanEn.c.o -o 1.02_BritishEn_to_AmericanEn -lfl 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # yacc_bison_practice 2 | 学习yacc和bison的练习程序,代码注释详尽 3 | 4 | ### ch1 5 | 本章主要对flex和bison相关功能的前瞻介绍,以及简单的实践 6 | 7 | ### ch2 8 | 详尽介绍flex的用法和功能,以及进一步的实践例子 9 | 10 | ### ch3 11 | 详尽介绍bison的用法和功能,以及进一步的实践例子 -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.05/CMakeFiles/3.05.dir/link.txt: -------------------------------------------------------------------------------- 1 | /usr/bin/c++ -g CMakeFiles/3.05.dir/main.cpp.o CMakeFiles/3.05.dir/driver.cpp.o CMakeFiles/3.05.dir/parser.cpp.o CMakeFiles/3.05.dir/lexer.cpp.o -o 3.05 -lm 2 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.03_recognize_calculator_tokens.dir/link.txt: -------------------------------------------------------------------------------- 1 | /usr/bin/cc -g CMakeFiles/1.03_recognize_calculator_tokens.dir/1.03_recognize_calculator_tokens.c.o -o 1.03_recognize_calculator_tokens -lfl 2 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.06_include_file_and_start_state.dir/link.txt: -------------------------------------------------------------------------------- 1 | /usr/bin/cc -g CMakeFiles/2.06_include_file_and_start_state.dir/2.06_include_file_and_start_state.c.o -o 2.06_include_file_and_start_state 2 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/CMakeFiles/3.05.dir/depend.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | ch3/CMakeFiles/3.05.dir/__/3.05lexer.c.o: 3.05lexer.c 5 | 6 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.02_use_yyin_to_read_data_from_file.dir/link.txt: -------------------------------------------------------------------------------- 1 | /usr/bin/cc -g CMakeFiles/2.02_use_yyin_to_read_data_from_file.dir/2.02_use_yyin_to_read_data_from_file.c.o -o 2.02_use_yyin_to_read_data_from_file 2 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.01_word_counter.dir/depend.internal: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | ch1/CMakeFiles/1.01_word_counter.dir/1.01_word_counter.c.o 5 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.05/CMakeFiles/3.05_bak.dir/link.txt: -------------------------------------------------------------------------------- 1 | /usr/bin/c++ -g CMakeFiles/3.05_bak.dir/main.cpp.o CMakeFiles/3.05_bak.dir/driver.cpp.o CMakeFiles/3.05_bak.dir/parser.cpp.o CMakeFiles/3.05_bak.dir/lexer.cpp.o -o 3.05_bak -lm 2 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.01_word_counter.dir/C.includecache: -------------------------------------------------------------------------------- 1 | #IncludeRegexLine: ^[ ]*[#%][ ]*(include|import)[ ]*[<"]([^">]+)([">]) 2 | 3 | #IncludeRegexScan: ^.*$ 4 | 5 | #IncludeRegexComplain: ^$ 6 | 7 | #IncludeRegexTransform: 8 | 9 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.03_read_files.dir/depend.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | ch2/CMakeFiles/2.03_read_files.dir/2.03_read_files.c.o: ch2/2.03_read_files.c 5 | 6 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/C.includecache: -------------------------------------------------------------------------------- 1 | #IncludeRegexLine: ^[ ]*[#%][ ]*(include|import)[ ]*[<"]([^">]+)([">]) 2 | 3 | #IncludeRegexScan: ^.*$ 4 | 5 | #IncludeRegexComplain: ^$ 6 | 7 | #IncludeRegexTransform: 8 | 9 | -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/test.dir/flags.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # compile C with /usr/bin/cc 5 | C_FLAGS = -g 6 | 7 | C_DEFINES = 8 | 9 | C_INCLUDES = 10 | 11 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.03_recognize_calculator_tokens.dir/C.includecache: -------------------------------------------------------------------------------- 1 | #IncludeRegexLine: ^[ ]*[#%][ ]*(include|import)[ ]*[<"]([^">]+)([">]) 2 | 3 | #IncludeRegexScan: ^.*$ 4 | 5 | #IncludeRegexComplain: ^$ 6 | 7 | #IncludeRegexTransform: 8 | 9 | -------------------------------------------------------------------------------- /test.l: -------------------------------------------------------------------------------- 1 | %option noyywrap 2 | 3 | %{ 4 | #include 5 | %} 6 | 7 | %% 8 | abc/de { printf("右上下文相关,匹配到了abc. yytext=%s, yyleng=%d\n",yytext, yyleng); } 9 | de { printf("匹配到了de\n"); } 10 | %% 11 | 12 | int main(){ 13 | yylex(); 14 | return 0; 15 | } -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/depend.internal: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/1.02_BritishEn_to_AmericanEn.c.o 5 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/CMakeFiles/3.05.dir/flags.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # compile C with /usr/bin/cc 5 | C_FLAGS = -g 6 | 7 | C_DEFINES = 8 | 9 | C_INCLUDES = 10 | 11 | -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/test.dir/depend.internal: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | CMakeFiles/test.dir/test.c.o 5 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/test.c 6 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.03_read_files.dir/flags.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # compile C with /usr/bin/cc 5 | C_FLAGS = -g 6 | 7 | C_DEFINES = 8 | 9 | C_INCLUDES = 10 | 11 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.03_recognize_calculator_tokens.dir/depend.internal: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | ch1/CMakeFiles/1.03_recognize_calculator_tokens.dir/1.03_recognize_calculator_tokens.c.o 5 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/depend.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/2.01_usual_lang_lex_re.c.o: ch2/2.01_usual_lang_lex_re.c 5 | 6 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/flags.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # compile C with /usr/bin/cc 5 | C_FLAGS = -g 6 | 7 | C_DEFINES = 8 | 9 | C_INCLUDES = 10 | 11 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/CMakeFiles/3.05.dir/depend.internal: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | ch3/CMakeFiles/3.05.dir/__/3.05lexer.c.o 5 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/3.05lexer.c 6 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.06_include_file_and_start_state.dir/flags.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # compile C with /usr/bin/cc 5 | C_FLAGS = -g 6 | 7 | C_DEFINES = 8 | 9 | C_INCLUDES = 10 | 11 | -------------------------------------------------------------------------------- /ch3/3.05/main.cpp: -------------------------------------------------------------------------------- 1 | // file: main.cpp 2 | // Created by cmp on 2020/5/11. 3 | // 4 | 5 | #include 6 | #include "driver.h" 7 | 8 | using namespace Marker; 9 | 10 | int main(int argc, char const *argv[]) { 11 | /* code */ 12 | Driver driver; 13 | driver.parse(); 14 | return 0; 15 | } -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.02_use_yyin_to_read_data_from_file.dir/flags.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # compile C with /usr/bin/cc 5 | C_FLAGS = -g 6 | 7 | C_DEFINES = 8 | 9 | C_INCLUDES = 10 | 11 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.02/CMakeFiles/3.02_create_AST_with_bison.dir/link.txt: -------------------------------------------------------------------------------- 1 | /usr/bin/cc -g CMakeFiles/3.02_create_AST_with_bison.dir/fb3.02.c.o CMakeFiles/3.02_create_AST_with_bison.dir/3.02lexer.c.o CMakeFiles/3.02_create_AST_with_bison.dir/3.02_create_AST_with_bison.tab.c.o -o 3.02_create_AST_with_bison 2 | -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/test.dir/cmake_clean.cmake: -------------------------------------------------------------------------------- 1 | file(REMOVE_RECURSE 2 | "CMakeFiles/test.dir/test.c.o" 3 | "test" 4 | "test.pdb" 5 | ) 6 | 7 | # Per-language clean rules from dependency scanning. 8 | foreach(lang C) 9 | include(CMakeFiles/test.dir/cmake_clean_${lang}.cmake OPTIONAL) 10 | endforeach() 11 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.03_read_files.dir/depend.internal: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | ch2/CMakeFiles/2.03_read_files.dir/2.03_read_files.c.o 5 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/2.03_read_files.c 6 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.06_include_file_and_start_state.dir/depend.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | ch2/CMakeFiles/2.06_include_file_and_start_state.dir/2.06_include_file_and_start_state.c.o: ch2/2.06_include_file_and_start_state.c 5 | 6 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.02_use_yyin_to_read_data_from_file.dir/depend.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | ch2/CMakeFiles/2.02_use_yyin_to_read_data_from_file.dir/2.02_use_yyin_to_read_data_from_file.c.o: ch2/2.02_use_yyin_to_read_data_from_file.c 5 | 6 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/CMakeFiles/3.05.dir/cmake_clean.cmake: -------------------------------------------------------------------------------- 1 | file(REMOVE_RECURSE 2 | "3.05" 3 | "3.05.pdb" 4 | "CMakeFiles/3.05.dir/__/3.05lexer.c.o" 5 | ) 6 | 7 | # Per-language clean rules from dependency scanning. 8 | foreach(lang C) 9 | include(CMakeFiles/3.05.dir/cmake_clean_${lang}.cmake OPTIONAL) 10 | endforeach() 11 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/depend.internal: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/1.04_main.c.o 5 | /home/cmp/work_dir/source_code/yacc_bison_practice/ch1/1.04_main.c 6 | ch1/1.04_NextToken_with_Flex.h 7 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/depend.internal: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/2.01_usual_lang_lex_re.c.o 5 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/2.01_usual_lang_lex_re.c 6 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.01_word_counter.dir/flags.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # compile C with /usr/bin/cc 5 | C_FLAGS = -g 6 | 7 | C_DEFINES = 8 | 9 | C_INCLUDES = -I/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1 10 | 11 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/depend.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/1.04_main.c.o: ../ch1/1.04_main.c 5 | ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/1.04_main.c.o: ch1/1.04_NextToken_with_Flex.h 6 | 7 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/flags.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # compile C with /usr/bin/cc 5 | C_FLAGS = -g 6 | 7 | C_DEFINES = 8 | 9 | C_INCLUDES = -I/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1 10 | 11 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/flags.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # compile C with /usr/bin/cc 5 | C_FLAGS = -g 6 | 7 | C_DEFINES = 8 | 9 | C_INCLUDES = -I/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1 10 | 11 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.03_recognize_calculator_tokens.dir/flags.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # compile C with /usr/bin/cc 5 | C_FLAGS = -g 6 | 7 | C_DEFINES = 8 | 9 | C_INCLUDES = -I/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1 10 | 11 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.03_read_files.dir/cmake_clean.cmake: -------------------------------------------------------------------------------- 1 | file(REMOVE_RECURSE 2 | "2.03_read_files" 3 | "2.03_read_files.pdb" 4 | "CMakeFiles/2.03_read_files.dir/2.03_read_files.c.o" 5 | ) 6 | 7 | # Per-language clean rules from dependency scanning. 8 | foreach(lang C) 9 | include(CMakeFiles/2.03_read_files.dir/cmake_clean_${lang}.cmake OPTIONAL) 10 | endforeach() 11 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.06_include_file_and_start_state.dir/depend.internal: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | ch2/CMakeFiles/2.06_include_file_and_start_state.dir/2.06_include_file_and_start_state.c.o 5 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/2.06_include_file_and_start_state.c 6 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/1.06/CMakeFiles/1.06.dir/cmake_clean.cmake: -------------------------------------------------------------------------------- 1 | file(REMOVE_RECURSE 2 | "1.06" 3 | "1.06.pdb" 4 | "CMakeFiles/1.06.dir/1.06_calculator_with_bison.tab.c.o" 5 | "CMakeFiles/1.06.dir/1.06lexer.c.o" 6 | ) 7 | 8 | # Per-language clean rules from dependency scanning. 9 | foreach(lang C) 10 | include(CMakeFiles/1.06.dir/cmake_clean_${lang}.cmake OPTIONAL) 11 | endforeach() 12 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.01_word_counter.dir/cmake_clean.cmake: -------------------------------------------------------------------------------- 1 | file(REMOVE_RECURSE 2 | "1.01_word_counter" 3 | "1.01_word_counter.pdb" 4 | "CMakeFiles/1.01_word_counter.dir/1.01_word_counter.c.o" 5 | ) 6 | 7 | # Per-language clean rules from dependency scanning. 8 | foreach(lang C) 9 | include(CMakeFiles/1.01_word_counter.dir/cmake_clean_${lang}.cmake OPTIONAL) 10 | endforeach() 11 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.02_use_yyin_to_read_data_from_file.dir/depend.internal: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | ch2/CMakeFiles/2.02_use_yyin_to_read_data_from_file.dir/2.02_use_yyin_to_read_data_from_file.c.o 5 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/2.02_use_yyin_to_read_data_from_file.c 6 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/1.06/CMakeFiles/1.06.dir/flags.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # compile C with /usr/bin/cc 5 | C_FLAGS = -g 6 | 7 | C_DEFINES = 8 | 9 | C_INCLUDES = -I/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1 -I/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug 10 | 11 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.05/CMakeFiles/3.05.dir/flags.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # compile CXX with /usr/bin/c++ 5 | CXX_FLAGS = -g 6 | 7 | CXX_DEFINES = 8 | 9 | CXX_INCLUDES = -I/home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.05 -I/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05 10 | 11 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.05/CMakeFiles/3.05_bak.dir/flags.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # compile CXX with /usr/bin/c++ 5 | CXX_FLAGS = -g 6 | 7 | CXX_DEFINES = 8 | 9 | CXX_INCLUDES = -I/home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.05 -I/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05 10 | 11 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/cmake_clean.cmake: -------------------------------------------------------------------------------- 1 | file(REMOVE_RECURSE 2 | "1.04_NextToken_with_Flex" 3 | "1.04_NextToken_with_Flex.pdb" 4 | "CMakeFiles/1.04_NextToken_with_Flex.dir/1.04_main.c.o" 5 | ) 6 | 7 | # Per-language clean rules from dependency scanning. 8 | foreach(lang C) 9 | include(CMakeFiles/1.04_NextToken_with_Flex.dir/cmake_clean_${lang}.cmake OPTIONAL) 10 | endforeach() 11 | -------------------------------------------------------------------------------- /ch3/3.05/driver.cpp: -------------------------------------------------------------------------------- 1 | // file: driver.cpp 2 | // Created by cmp on 2020/5/11. 3 | // 4 | 5 | #include "driver.h" 6 | #include 7 | 8 | using namespace Marker; 9 | using namespace std; 10 | 11 | Driver::Driver() : _scanner(*this), _parser(_scanner, *this) { 12 | 13 | } 14 | 15 | Driver::~Driver() { 16 | 17 | } 18 | 19 | int Driver::parse() { 20 | _scanner.switch_streams(cin, cout); 21 | return _parser.parse(); 22 | } -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/cmake_clean.cmake: -------------------------------------------------------------------------------- 1 | file(REMOVE_RECURSE 2 | "2.01_usual_lang_lex_re" 3 | "2.01_usual_lang_lex_re.pdb" 4 | "CMakeFiles/2.01_usual_lang_lex_re.dir/2.01_usual_lang_lex_re.c.o" 5 | ) 6 | 7 | # Per-language clean rules from dependency scanning. 8 | foreach(lang C) 9 | include(CMakeFiles/2.01_usual_lang_lex_re.dir/cmake_clean_${lang}.cmake OPTIONAL) 10 | endforeach() 11 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.02/CMakeFiles/3.02_create_AST_with_bison.dir/flags.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # compile C with /usr/bin/cc 5 | C_FLAGS = -g 6 | 7 | C_DEFINES = 8 | 9 | C_INCLUDES = -I/home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.02 -I/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.02 10 | 11 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/1.06/CMakeFiles/1.06.dir/depend.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | ch1/1.06/CMakeFiles/1.06.dir/1.06_calculator_with_bison.tab.c.o: ch1/1.06/1.06_calculator_with_bison.tab.c 5 | 6 | ch1/1.06/CMakeFiles/1.06.dir/1.06lexer.c.o: ch1/1.06/1.06_calculator_with_bison.tab.h 7 | ch1/1.06/CMakeFiles/1.06.dir/1.06lexer.c.o: ch1/1.06/1.06lexer.c 8 | 9 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/CMakeFiles/3.05.dir/C.includecache: -------------------------------------------------------------------------------- 1 | #IncludeRegexLine: ^[ ]*[#%][ ]*(include|import)[ ]*[<"]([^">]+)([">]) 2 | 3 | #IncludeRegexScan: ^.*$ 4 | 5 | #IncludeRegexComplain: ^$ 6 | 7 | #IncludeRegexTransform: 8 | 9 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/3.05lexer.c 10 | stdio.h 11 | - 12 | string.h 13 | - 14 | errno.h 15 | - 16 | stdlib.h 17 | - 18 | inttypes.h 19 | - 20 | unistd.h 21 | - 22 | 23 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.05/CMakeFiles/3.05.dir/cmake_clean.cmake: -------------------------------------------------------------------------------- 1 | file(REMOVE_RECURSE 2 | "3.05" 3 | "3.05.pdb" 4 | "CMakeFiles/3.05.dir/driver.cpp.o" 5 | "CMakeFiles/3.05.dir/lexer.cpp.o" 6 | "CMakeFiles/3.05.dir/main.cpp.o" 7 | "CMakeFiles/3.05.dir/parser.cpp.o" 8 | ) 9 | 10 | # Per-language clean rules from dependency scanning. 11 | foreach(lang CXX) 12 | include(CMakeFiles/3.05.dir/cmake_clean_${lang}.cmake OPTIONAL) 13 | endforeach() 14 | -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/test.dir/C.includecache: -------------------------------------------------------------------------------- 1 | #IncludeRegexLine: ^[ ]*[#%][ ]*(include|import)[ ]*[<"]([^">]+)([">]) 2 | 3 | #IncludeRegexScan: ^.*$ 4 | 5 | #IncludeRegexComplain: ^$ 6 | 7 | #IncludeRegexTransform: 8 | 9 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/test.c 10 | stdio.h 11 | - 12 | string.h 13 | - 14 | errno.h 15 | - 16 | stdlib.h 17 | - 18 | inttypes.h 19 | - 20 | stdio.h 21 | - 22 | unistd.h 23 | - 24 | 25 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/cmake_clean.cmake: -------------------------------------------------------------------------------- 1 | file(REMOVE_RECURSE 2 | "1.02_BritishEn_to_AmericanEn" 3 | "1.02_BritishEn_to_AmericanEn.pdb" 4 | "CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/1.02_BritishEn_to_AmericanEn.c.o" 5 | ) 6 | 7 | # Per-language clean rules from dependency scanning. 8 | foreach(lang C) 9 | include(CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/cmake_clean_${lang}.cmake OPTIONAL) 10 | endforeach() 11 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.03_read_files.dir/C.includecache: -------------------------------------------------------------------------------- 1 | #IncludeRegexLine: ^[ ]*[#%][ ]*(include|import)[ ]*[<"]([^">]+)([">]) 2 | 3 | #IncludeRegexScan: ^.*$ 4 | 5 | #IncludeRegexComplain: ^$ 6 | 7 | #IncludeRegexTransform: 8 | 9 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/2.03_read_files.c 10 | stdio.h 11 | - 12 | string.h 13 | - 14 | errno.h 15 | - 16 | stdlib.h 17 | - 18 | inttypes.h 19 | - 20 | unistd.h 21 | - 22 | 23 | -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/clion-log.txt: -------------------------------------------------------------------------------- 1 | /home/cmp/Downloads/clion-2019.2/bin/cmake/linux/bin/cmake -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - Unix Makefiles" /home/cmp/work_dir/source_code/yacc_bison_practice 2 | /home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.05/parser.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr] 3 | -- Configuring done 4 | -- Generating done 5 | -- Build files have been written to: /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug 6 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.05/CMakeFiles/3.05_bak.dir/cmake_clean.cmake: -------------------------------------------------------------------------------- 1 | file(REMOVE_RECURSE 2 | "3.05_bak" 3 | "3.05_bak.pdb" 4 | "CMakeFiles/3.05_bak.dir/driver.cpp.o" 5 | "CMakeFiles/3.05_bak.dir/lexer.cpp.o" 6 | "CMakeFiles/3.05_bak.dir/main.cpp.o" 7 | "CMakeFiles/3.05_bak.dir/parser.cpp.o" 8 | ) 9 | 10 | # Per-language clean rules from dependency scanning. 11 | foreach(lang CXX) 12 | include(CMakeFiles/3.05_bak.dir/cmake_clean_${lang}.cmake OPTIONAL) 13 | endforeach() 14 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.03_recognize_calculator_tokens.dir/cmake_clean.cmake: -------------------------------------------------------------------------------- 1 | file(REMOVE_RECURSE 2 | "1.03_recognize_calculator_tokens" 3 | "1.03_recognize_calculator_tokens.pdb" 4 | "CMakeFiles/1.03_recognize_calculator_tokens.dir/1.03_recognize_calculator_tokens.c.o" 5 | ) 6 | 7 | # Per-language clean rules from dependency scanning. 8 | foreach(lang C) 9 | include(CMakeFiles/1.03_recognize_calculator_tokens.dir/cmake_clean_${lang}.cmake OPTIONAL) 10 | endforeach() 11 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.06_include_file_and_start_state.dir/cmake_clean.cmake: -------------------------------------------------------------------------------- 1 | file(REMOVE_RECURSE 2 | "2.06_include_file_and_start_state" 3 | "2.06_include_file_and_start_state.pdb" 4 | "CMakeFiles/2.06_include_file_and_start_state.dir/2.06_include_file_and_start_state.c.o" 5 | ) 6 | 7 | # Per-language clean rules from dependency scanning. 8 | foreach(lang C) 9 | include(CMakeFiles/2.06_include_file_and_start_state.dir/cmake_clean_${lang}.cmake OPTIONAL) 10 | endforeach() 11 | -------------------------------------------------------------------------------- /ch3/3.05/driver.h: -------------------------------------------------------------------------------- 1 | // file:driver.h 2 | // Created by cmp on 2020/5/11. 3 | // 4 | 5 | #ifndef FLEX_BISON_DRIVER_H 6 | #define FLEX_BISON_DRIVER_H 7 | 8 | #include 9 | #include "scanner.h" 10 | #include "parser.hpp" 11 | 12 | namespace Marker { 13 | class Driver { 14 | private: 15 | Parser _parser; 16 | Scanner _scanner; 17 | public: 18 | Driver(); 19 | 20 | int parse(); 21 | 22 | virtual ~Driver(); 23 | }; 24 | } /*Marker */ 25 | #endif //FLEX_BISON_DRIVER_H 26 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/C.includecache: -------------------------------------------------------------------------------- 1 | #IncludeRegexLine: ^[ ]*[#%][ ]*(include|import)[ ]*[<"]([^">]+)([">]) 2 | 3 | #IncludeRegexScan: ^.*$ 4 | 5 | #IncludeRegexComplain: ^$ 6 | 7 | #IncludeRegexTransform: 8 | 9 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/2.01_usual_lang_lex_re.c 10 | stdio.h 11 | - 12 | string.h 13 | - 14 | errno.h 15 | - 16 | stdlib.h 17 | - 18 | inttypes.h 19 | - 20 | stdio.h 21 | - 22 | unistd.h 23 | - 24 | 25 | -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/3.16.5/CMakeSystem.cmake: -------------------------------------------------------------------------------- 1 | set(CMAKE_HOST_SYSTEM "Linux-5.0.0-37-generic") 2 | set(CMAKE_HOST_SYSTEM_NAME "Linux") 3 | set(CMAKE_HOST_SYSTEM_VERSION "5.0.0-37-generic") 4 | set(CMAKE_HOST_SYSTEM_PROCESSOR "x86_64") 5 | 6 | 7 | 8 | set(CMAKE_SYSTEM "Linux-5.0.0-37-generic") 9 | set(CMAKE_SYSTEM_NAME "Linux") 10 | set(CMAKE_SYSTEM_VERSION "5.0.0-37-generic") 11 | set(CMAKE_SYSTEM_PROCESSOR "x86_64") 12 | 13 | set(CMAKE_CROSSCOMPILING "FALSE") 14 | 15 | set(CMAKE_SYSTEM_LOADED 1) 16 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.02_use_yyin_to_read_data_from_file.dir/cmake_clean.cmake: -------------------------------------------------------------------------------- 1 | file(REMOVE_RECURSE 2 | "2.02_use_yyin_to_read_data_from_file" 3 | "2.02_use_yyin_to_read_data_from_file.pdb" 4 | "CMakeFiles/2.02_use_yyin_to_read_data_from_file.dir/2.02_use_yyin_to_read_data_from_file.c.o" 5 | ) 6 | 7 | # Per-language clean rules from dependency scanning. 8 | foreach(lang C) 9 | include(CMakeFiles/2.02_use_yyin_to_read_data_from_file.dir/cmake_clean_${lang}.cmake OPTIONAL) 10 | endforeach() 11 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.06_include_file_and_start_state.dir/C.includecache: -------------------------------------------------------------------------------- 1 | #IncludeRegexLine: ^[ ]*[#%][ ]*(include|import)[ ]*[<"]([^">]+)([">]) 2 | 3 | #IncludeRegexScan: ^.*$ 4 | 5 | #IncludeRegexComplain: ^$ 6 | 7 | #IncludeRegexTransform: 8 | 9 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/2.06_include_file_and_start_state.c 10 | stdio.h 11 | - 12 | string.h 13 | - 14 | errno.h 15 | - 16 | stdlib.h 17 | - 18 | inttypes.h 19 | - 20 | unistd.h 21 | - 22 | 23 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.02_use_yyin_to_read_data_from_file.dir/C.includecache: -------------------------------------------------------------------------------- 1 | #IncludeRegexLine: ^[ ]*[#%][ ]*(include|import)[ ]*[<"]([^">]+)([">]) 2 | 3 | #IncludeRegexScan: ^.*$ 4 | 5 | #IncludeRegexComplain: ^$ 6 | 7 | #IncludeRegexTransform: 8 | 9 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/2.02_use_yyin_to_read_data_from_file.c 10 | stdio.h 11 | - 12 | string.h 13 | - 14 | errno.h 15 | - 16 | stdlib.h 17 | - 18 | inttypes.h 19 | - 20 | unistd.h 21 | - 22 | 23 | -------------------------------------------------------------------------------- /ch1/1.02_BritishEn_to_AmericanEn.l: -------------------------------------------------------------------------------- 1 | /* 2 | // file: 1.02_BritishEn_to_AmericanEn.y 3 | // 将英式英语转为美式英语 4 | */ 5 | %% 6 | "colour" { printf("color"); } // 当匹配到了colour单词,将打印处color 7 | "flavour" { printf("flavor"); } // 当匹配到了flavour单词,将打印处flavor 8 | "clever" { printf("smart"); } // 当匹配到了clever单词,将打印处smart 9 | "smart" { printf("elegant"); } // 当匹配到了smart单词,将打印处elegant 10 | "conservative" { printf("liberal"); } // 当匹配到了conservative单词,将打印处liberal 11 | "." { printf("%s", yytext); } // 当匹配到其他任意字符,将原样输出 12 | %% -------------------------------------------------------------------------------- /ch1/README.md: -------------------------------------------------------------------------------- 1 | # 第一章 2 | ## CH1 内容概览 3 | 4 | ### 1.01 5 | 6 | 一个字数统计(word_counter)程序,程序读入一个文件然后报告这个文件的行数、单词树和字符数。 7 | 8 | ### 1.02 9 | 10 | 将英式英语转为美式英语 11 | 12 | ### 1.03 13 | 14 | 用于将输入的计算表达式识别为token流,然后输入 15 | 16 | ### 1.04 17 | 18 | 在词法分析的时候,经常我们需要一个NextToken的函数,这个函数每一次调用返回下一个识别了的Token 19 | 20 | 本例子就是结合Flex来实现NextToken函数的基本功能,来识别C语言中的token 21 | 22 | 该词法分析器仅支持标准C98的关键字识别,新的标准增加的关键字并不支持 23 | 24 | 注意:该词法分析器并不支持对字符串的识别,如:"3.2.4.5.12..5." 25 | 26 | ### 1.05 27 | 28 | 利用Bison实现四则运算的语法解析器 29 | 30 | ### 1.06 31 | 32 | 使用Flex和Bison手写词法分析器和语法分析器,实现一个简单的计算器 33 | 34 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.02/CMakeFiles/3.02_create_AST_with_bison.dir/cmake_clean.cmake: -------------------------------------------------------------------------------- 1 | file(REMOVE_RECURSE 2 | "3.02_create_AST_with_bison" 3 | "3.02_create_AST_with_bison.pdb" 4 | "CMakeFiles/3.02_create_AST_with_bison.dir/3.02_create_AST_with_bison.tab.c.o" 5 | "CMakeFiles/3.02_create_AST_with_bison.dir/3.02lexer.c.o" 6 | "CMakeFiles/3.02_create_AST_with_bison.dir/fb3.02.c.o" 7 | ) 8 | 9 | # Per-language clean rules from dependency scanning. 10 | foreach(lang C) 11 | include(CMakeFiles/3.02_create_AST_with_bison.dir/cmake_clean_${lang}.cmake OPTIONAL) 12 | endforeach() 13 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/1.06/CMakeFiles/1.06.dir/depend.internal: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | ch1/1.06/CMakeFiles/1.06.dir/1.06_calculator_with_bison.tab.c.o 5 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/1.06/1.06_calculator_with_bison.tab.c 6 | ch1/1.06/CMakeFiles/1.06.dir/1.06lexer.c.o 7 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/1.06/1.06_calculator_with_bison.tab.h 8 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/1.06/1.06lexer.c 9 | -------------------------------------------------------------------------------- /ch3/3.02/3.02lexer.l: -------------------------------------------------------------------------------- 1 | /* 不使用-lfl定义的默认main函数,使用自定义的main函数,你就不需要链接-lfl了。 */ 2 | %option noyywrap 3 | 4 | %{ 5 | #include "fb3.02.h" 6 | #include "3.02_create_AST_with_bison.tab.h" 7 | %} 8 | 9 | /* 此处声明模式的公共部分,用于简化匹配模式的书写。*/ 10 | /* 浮点数指数部分 */ 11 | EXP ([Ee][-+]?[0-9]+) 12 | 13 | %% 14 | "+" | 15 | "-" | 16 | "*" | 17 | "/" | 18 | "|" | 19 | "(" | 20 | ")" { return yytext[0]; } 21 | 22 | [0-9]+"."[0-9]*{EXP}?|"."?[0-9]+{EXP}? { yylval.d = atof(yytext); return NUMBER; } 23 | 24 | \n { return EOL; } 25 | "//".*\n 26 | [ \t] { /* 忽略空白字符 */} 27 | . { yyerror("Mystery character %c\n", *yytext); } 28 | %% -------------------------------------------------------------------------------- /ch1/1.03_recognize_calculator_tokens.l: -------------------------------------------------------------------------------- 1 | /* 2 | // file: 1.03_recognize_calculator_tokens.y 3 | // 用于将输入的计算表达式识别为token流,然后输入 4 | */ 5 | 6 | /* 7 | 前5个匹配模式,使用了双引号,flex将使用引号内的文本而不会将其解释为正则表达式。 8 | */ 9 | %% 10 | "+" { printf("PLUS\n"); } // 当匹配上了+,将打印PLUS 11 | "-" { printf("MINUS\n"); } 12 | "*" { printf("TIMES\n"); } 13 | "/" { printf("DIVIDE\n"); } 14 | "|" { printf("ABS\n"); } 15 | [0-9]+ { printf("NUMBER %s\n", yytext); } // yytext变量总是指向本次匹配的输入文本字符串 16 | \n { printf("NEWLINE\n"); } 17 | [ \t] {} 18 | . { printf("Mystery character %s\n", yytext); } // yytext变量总是指向本次匹配的输入文本字符串 19 | %% -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/C.includecache: -------------------------------------------------------------------------------- 1 | #IncludeRegexLine: ^[ ]*[#%][ ]*(include|import)[ ]*[<"]([^">]+)([">]) 2 | 3 | #IncludeRegexScan: ^.*$ 4 | 5 | #IncludeRegexComplain: ^$ 6 | 7 | #IncludeRegexTransform: 8 | 9 | /home/cmp/work_dir/source_code/yacc_bison_practice/ch1/1.04_main.c 10 | stdbool.h 11 | - 12 | stdio.h 13 | - 14 | 1.04_NextToken_with_Flex.h 15 | /home/cmp/work_dir/source_code/yacc_bison_practice/ch1/1.04_NextToken_with_Flex.h 16 | 17 | ch1/1.04_NextToken_with_Flex.h 18 | stdio.h 19 | - 20 | string.h 21 | - 22 | errno.h 23 | - 24 | stdlib.h 25 | - 26 | inttypes.h 27 | - 28 | unistd.h 29 | - 30 | 31 | -------------------------------------------------------------------------------- /cmake/clean_flex_bison_generate_code.cmake: -------------------------------------------------------------------------------- 1 | 2 | 3 | #execute_process(COMMAND find ${CMAKE_CURRENT_BINARY_DIR} -type f -perm -111 -exec rm -f {} \;) # 清除所有可执行文件 4 | execute_process(COMMAND find ${CMAKE_CURRENT_BINARY_DIR} -type f -name "*.h" -exec rm -f {} \;) 5 | execute_process(COMMAND find ${CMAKE_CURRENT_BINARY_DIR} -type f -name "*.c" -exec rm -f {} \;) 6 | execute_process(COMMAND find ${CMAKE_CURRENT_BINARY_DIR} -type f -name "*.cc" -exec rm -f {} \;) 7 | execute_process(COMMAND find ${CMAKE_CURRENT_BINARY_DIR} -type f -name "*.hh" -exec rm -f {} \;) 8 | execute_process(COMMAND find ${CMAKE_CURRENT_BINARY_DIR} -type f -name "*.hpp" -exec rm -f {} \;) 9 | execute_process(COMMAND find ${CMAKE_CURRENT_BINARY_DIR} -type f -name "*.cpp" -exec rm -f {} \;) 10 | -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/test.dir/DependInfo.cmake: -------------------------------------------------------------------------------- 1 | # The set of languages for which implicit dependencies are needed: 2 | set(CMAKE_DEPENDS_LANGUAGES 3 | "C" 4 | ) 5 | # The set of files for implicit dependencies of each language: 6 | set(CMAKE_DEPENDS_CHECK_C 7 | "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/test.c" "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/CMakeFiles/test.dir/test.c.o" 8 | ) 9 | set(CMAKE_C_COMPILER_ID "GNU") 10 | 11 | # The include file search paths: 12 | set(CMAKE_C_TARGET_INCLUDE_PATH 13 | ) 14 | 15 | # Targets to which this target links. 16 | set(CMAKE_TARGET_LINKED_INFO_FILES 17 | ) 18 | 19 | # Fortran module output directory. 20 | set(CMAKE_Fortran_TARGET_MODULE_DIR "") 21 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/CMakeFiles/3.05.dir/DependInfo.cmake: -------------------------------------------------------------------------------- 1 | # The set of languages for which implicit dependencies are needed: 2 | set(CMAKE_DEPENDS_LANGUAGES 3 | "C" 4 | ) 5 | # The set of files for implicit dependencies of each language: 6 | set(CMAKE_DEPENDS_CHECK_C 7 | "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/3.05lexer.c" "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/CMakeFiles/3.05.dir/__/3.05lexer.c.o" 8 | ) 9 | set(CMAKE_C_COMPILER_ID "GNU") 10 | 11 | # The include file search paths: 12 | set(CMAKE_C_TARGET_INCLUDE_PATH 13 | ) 14 | 15 | # Targets to which this target links. 16 | set(CMAKE_TARGET_LINKED_INFO_FILES 17 | ) 18 | 19 | # Fortran module output directory. 20 | set(CMAKE_Fortran_TARGET_MODULE_DIR "") 21 | -------------------------------------------------------------------------------- /ch3/3.02/fb3.02.h: -------------------------------------------------------------------------------- 1 | /* 计算器的声明部分 */ 2 | 3 | /* 与词法分析器的接口 */ 4 | extern int yylineno; 5 | 6 | void yyerror(char *s, ...); 7 | 8 | enum NodeT { // 声明所有语法树节点类型的枚举值 9 | NT_UNDEF, 10 | NT_ADD, 11 | NT_SUB, 12 | NT_MUL, 13 | NT_DIV, 14 | NT_ABS, 15 | NT_NEG, 16 | NT_NUM, 17 | }; 18 | 19 | /* 抽象语法树中的节点 */ 20 | struct ASTNode { 21 | int NodeType; 22 | struct ASTNode *Lft; 23 | struct ASTNode *Rht; 24 | }; 25 | 26 | struct numval { 27 | int NodeType; /* 类型K 表明常量 */ 28 | double number; 29 | }; 30 | 31 | /* 构造抽象语法树 */ 32 | struct ASTNode *newast(int NodeType, struct ASTNode *Lft, struct ASTNode *Rht); 33 | 34 | struct ASTNode *newnum(double d); 35 | 36 | /* 计算抽象语法树 */ 37 | double eval(struct ASTNode *); 38 | 39 | /* 删除和释放抽象语法树 */ 40 | void treefree(struct ASTNode *); 41 | 42 | -------------------------------------------------------------------------------- /ch3/3.05/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include(${PROJECT_SOURCE_DIR}/cmake/clean_flex_bison_generate_code.cmake) 2 | 3 | # 必须预先设置好包含目录,因为在fb3.04parser.y文件中有包含其他头文件,Bison生成代码时会校验包含的头文件是否能够找得到 4 | 5 | include_directories(${CMAKE_CURRENT_SOURCE_DIR} 6 | ${CMAKE_CURRENT_BINARY_DIR}) 7 | 8 | execute_process(COMMAND 9 | flex -+ -o ${CMAKE_CURRENT_BINARY_DIR}/lexer.cpp ${CMAKE_CURRENT_SOURCE_DIR}/lexer.l) 10 | 11 | execute_process(COMMAND 12 | bison -o ${CMAKE_CURRENT_BINARY_DIR}/parser.cpp ${CMAKE_CURRENT_SOURCE_DIR}/parser.y) 13 | 14 | 15 | add_executable(3.05 16 | ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp 17 | ${CMAKE_CURRENT_SOURCE_DIR}/driver.cpp 18 | ${CMAKE_CURRENT_BINARY_DIR}/parser.cpp 19 | ${CMAKE_CURRENT_BINARY_DIR}/lexer.cpp) 20 | 21 | target_link_libraries(3.05 PUBLIC 22 | -lm) -------------------------------------------------------------------------------- /ch1/1.06/1.06lexer.l: -------------------------------------------------------------------------------- 1 | /* 2 | // file: 1.06.y 3 | // 使用Flex和Bison手写词法分析器和语法分析器,实现一个简单的计算 4 | */ 5 | 6 | %{ 7 | # include "1.06_calculator_with_bison.tab.h" // 此处需要将语法分析器生成的头文件包含进来,这样Flex和bison就可以联合工作了 8 | %} 9 | 10 | %% 11 | "+" { return ADD; } 12 | "-" { return SUB; } 13 | "*" { return MUL; } 14 | "/" { return DIV; } 15 | "|" { return ABS; } 16 | "(" { return L_PARENTHESIS; } 17 | ")" { return R_PARENTHESIS; } 18 | 19 | [0-9]+ { yylval = atoi(yytext); return NUMBER; } // 识别整数 20 | \n { return EOL; } 21 | [ \t] { /*忽略空白字符*/ } 22 | . { printf("Mystery character %c\n", *yytext); } 23 | %% 24 | 25 | -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/CMakeDirectoryInformation.cmake: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # Relative path conversion top directories. 5 | set(CMAKE_RELATIVE_PATH_TOP_SOURCE "/home/cmp/work_dir/source_code/yacc_bison_practice") 6 | set(CMAKE_RELATIVE_PATH_TOP_BINARY "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug") 7 | 8 | # Force unix paths in dependencies. 9 | set(CMAKE_FORCE_UNIX_PATHS 1) 10 | 11 | 12 | # The C and CXX include file regular expressions for this directory. 13 | set(CMAKE_C_INCLUDE_REGEX_SCAN "^.*$") 14 | set(CMAKE_C_INCLUDE_REGEX_COMPLAIN "^$") 15 | set(CMAKE_CXX_INCLUDE_REGEX_SCAN ${CMAKE_C_INCLUDE_REGEX_SCAN}) 16 | set(CMAKE_CXX_INCLUDE_REGEX_COMPLAIN ${CMAKE_C_INCLUDE_REGEX_COMPLAIN}) 17 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/CMakeDirectoryInformation.cmake: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # Relative path conversion top directories. 5 | set(CMAKE_RELATIVE_PATH_TOP_SOURCE "/home/cmp/work_dir/source_code/yacc_bison_practice") 6 | set(CMAKE_RELATIVE_PATH_TOP_BINARY "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug") 7 | 8 | # Force unix paths in dependencies. 9 | set(CMAKE_FORCE_UNIX_PATHS 1) 10 | 11 | 12 | # The C and CXX include file regular expressions for this directory. 13 | set(CMAKE_C_INCLUDE_REGEX_SCAN "^.*$") 14 | set(CMAKE_C_INCLUDE_REGEX_COMPLAIN "^$") 15 | set(CMAKE_CXX_INCLUDE_REGEX_SCAN ${CMAKE_C_INCLUDE_REGEX_SCAN}) 16 | set(CMAKE_CXX_INCLUDE_REGEX_COMPLAIN ${CMAKE_C_INCLUDE_REGEX_COMPLAIN}) 17 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/CMakeDirectoryInformation.cmake: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # Relative path conversion top directories. 5 | set(CMAKE_RELATIVE_PATH_TOP_SOURCE "/home/cmp/work_dir/source_code/yacc_bison_practice") 6 | set(CMAKE_RELATIVE_PATH_TOP_BINARY "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug") 7 | 8 | # Force unix paths in dependencies. 9 | set(CMAKE_FORCE_UNIX_PATHS 1) 10 | 11 | 12 | # The C and CXX include file regular expressions for this directory. 13 | set(CMAKE_C_INCLUDE_REGEX_SCAN "^.*$") 14 | set(CMAKE_C_INCLUDE_REGEX_COMPLAIN "^$") 15 | set(CMAKE_CXX_INCLUDE_REGEX_SCAN ${CMAKE_C_INCLUDE_REGEX_SCAN}) 16 | set(CMAKE_CXX_INCLUDE_REGEX_COMPLAIN ${CMAKE_C_INCLUDE_REGEX_COMPLAIN}) 17 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/CMakeFiles/CMakeDirectoryInformation.cmake: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # Relative path conversion top directories. 5 | set(CMAKE_RELATIVE_PATH_TOP_SOURCE "/home/cmp/work_dir/source_code/yacc_bison_practice") 6 | set(CMAKE_RELATIVE_PATH_TOP_BINARY "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug") 7 | 8 | # Force unix paths in dependencies. 9 | set(CMAKE_FORCE_UNIX_PATHS 1) 10 | 11 | 12 | # The C and CXX include file regular expressions for this directory. 13 | set(CMAKE_C_INCLUDE_REGEX_SCAN "^.*$") 14 | set(CMAKE_C_INCLUDE_REGEX_COMPLAIN "^$") 15 | set(CMAKE_CXX_INCLUDE_REGEX_SCAN ${CMAKE_C_INCLUDE_REGEX_SCAN}) 16 | set(CMAKE_CXX_INCLUDE_REGEX_COMPLAIN ${CMAKE_C_INCLUDE_REGEX_COMPLAIN}) 17 | -------------------------------------------------------------------------------- /cmake-build-debug/ch5/CMakeFiles/CMakeDirectoryInformation.cmake: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # Relative path conversion top directories. 5 | set(CMAKE_RELATIVE_PATH_TOP_SOURCE "/home/cmp/work_dir/source_code/yacc_bison_practice") 6 | set(CMAKE_RELATIVE_PATH_TOP_BINARY "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug") 7 | 8 | # Force unix paths in dependencies. 9 | set(CMAKE_FORCE_UNIX_PATHS 1) 10 | 11 | 12 | # The C and CXX include file regular expressions for this directory. 13 | set(CMAKE_C_INCLUDE_REGEX_SCAN "^.*$") 14 | set(CMAKE_C_INCLUDE_REGEX_COMPLAIN "^$") 15 | set(CMAKE_CXX_INCLUDE_REGEX_SCAN ${CMAKE_C_INCLUDE_REGEX_SCAN}) 16 | set(CMAKE_CXX_INCLUDE_REGEX_COMPLAIN ${CMAKE_C_INCLUDE_REGEX_COMPLAIN}) 17 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/1.06/CMakeFiles/CMakeDirectoryInformation.cmake: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # Relative path conversion top directories. 5 | set(CMAKE_RELATIVE_PATH_TOP_SOURCE "/home/cmp/work_dir/source_code/yacc_bison_practice") 6 | set(CMAKE_RELATIVE_PATH_TOP_BINARY "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug") 7 | 8 | # Force unix paths in dependencies. 9 | set(CMAKE_FORCE_UNIX_PATHS 1) 10 | 11 | 12 | # The C and CXX include file regular expressions for this directory. 13 | set(CMAKE_C_INCLUDE_REGEX_SCAN "^.*$") 14 | set(CMAKE_C_INCLUDE_REGEX_COMPLAIN "^$") 15 | set(CMAKE_CXX_INCLUDE_REGEX_SCAN ${CMAKE_C_INCLUDE_REGEX_SCAN}) 16 | set(CMAKE_CXX_INCLUDE_REGEX_COMPLAIN ${CMAKE_C_INCLUDE_REGEX_COMPLAIN}) 17 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/DependInfo.cmake: -------------------------------------------------------------------------------- 1 | # The set of languages for which implicit dependencies are needed: 2 | set(CMAKE_DEPENDS_LANGUAGES 3 | "C" 4 | ) 5 | # The set of files for implicit dependencies of each language: 6 | set(CMAKE_DEPENDS_CHECK_C 7 | "/home/cmp/work_dir/source_code/yacc_bison_practice/ch1/1.04_main.c" "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/1.04_main.c.o" 8 | ) 9 | set(CMAKE_C_COMPILER_ID "GNU") 10 | 11 | # The include file search paths: 12 | set(CMAKE_C_TARGET_INCLUDE_PATH 13 | "ch1" 14 | ) 15 | 16 | # Targets to which this target links. 17 | set(CMAKE_TARGET_LINKED_INFO_FILES 18 | ) 19 | 20 | # Fortran module output directory. 21 | set(CMAKE_Fortran_TARGET_MODULE_DIR "") 22 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.03_read_files.dir/DependInfo.cmake: -------------------------------------------------------------------------------- 1 | # The set of languages for which implicit dependencies are needed: 2 | set(CMAKE_DEPENDS_LANGUAGES 3 | "C" 4 | ) 5 | # The set of files for implicit dependencies of each language: 6 | set(CMAKE_DEPENDS_CHECK_C 7 | "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/2.03_read_files.c" "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/CMakeFiles/2.03_read_files.dir/2.03_read_files.c.o" 8 | ) 9 | set(CMAKE_C_COMPILER_ID "GNU") 10 | 11 | # The include file search paths: 12 | set(CMAKE_C_TARGET_INCLUDE_PATH 13 | ) 14 | 15 | # Targets to which this target links. 16 | set(CMAKE_TARGET_LINKED_INFO_FILES 17 | ) 18 | 19 | # Fortran module output directory. 20 | set(CMAKE_Fortran_TARGET_MODULE_DIR "") 21 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.02/CMakeFiles/CMakeDirectoryInformation.cmake: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # Relative path conversion top directories. 5 | set(CMAKE_RELATIVE_PATH_TOP_SOURCE "/home/cmp/work_dir/source_code/yacc_bison_practice") 6 | set(CMAKE_RELATIVE_PATH_TOP_BINARY "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug") 7 | 8 | # Force unix paths in dependencies. 9 | set(CMAKE_FORCE_UNIX_PATHS 1) 10 | 11 | 12 | # The C and CXX include file regular expressions for this directory. 13 | set(CMAKE_C_INCLUDE_REGEX_SCAN "^.*$") 14 | set(CMAKE_C_INCLUDE_REGEX_COMPLAIN "^$") 15 | set(CMAKE_CXX_INCLUDE_REGEX_SCAN ${CMAKE_C_INCLUDE_REGEX_SCAN}) 16 | set(CMAKE_CXX_INCLUDE_REGEX_COMPLAIN ${CMAKE_C_INCLUDE_REGEX_COMPLAIN}) 17 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.05/CMakeFiles/CMakeDirectoryInformation.cmake: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # Relative path conversion top directories. 5 | set(CMAKE_RELATIVE_PATH_TOP_SOURCE "/home/cmp/work_dir/source_code/yacc_bison_practice") 6 | set(CMAKE_RELATIVE_PATH_TOP_BINARY "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug") 7 | 8 | # Force unix paths in dependencies. 9 | set(CMAKE_FORCE_UNIX_PATHS 1) 10 | 11 | 12 | # The C and CXX include file regular expressions for this directory. 13 | set(CMAKE_C_INCLUDE_REGEX_SCAN "^.*$") 14 | set(CMAKE_C_INCLUDE_REGEX_COMPLAIN "^$") 15 | set(CMAKE_CXX_INCLUDE_REGEX_SCAN ${CMAKE_C_INCLUDE_REGEX_SCAN}) 16 | set(CMAKE_CXX_INCLUDE_REGEX_COMPLAIN ${CMAKE_C_INCLUDE_REGEX_COMPLAIN}) 17 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.01_word_counter.dir/DependInfo.cmake: -------------------------------------------------------------------------------- 1 | # The set of languages for which implicit dependencies are needed: 2 | set(CMAKE_DEPENDS_LANGUAGES 3 | "C" 4 | ) 5 | # The set of files for implicit dependencies of each language: 6 | set(CMAKE_DEPENDS_CHECK_C 7 | "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/1.01_word_counter.c" "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/CMakeFiles/1.01_word_counter.dir/1.01_word_counter.c.o" 8 | ) 9 | set(CMAKE_C_COMPILER_ID "GNU") 10 | 11 | # The include file search paths: 12 | set(CMAKE_C_TARGET_INCLUDE_PATH 13 | "ch1" 14 | ) 15 | 16 | # Targets to which this target links. 17 | set(CMAKE_TARGET_LINKED_INFO_FILES 18 | ) 19 | 20 | # Fortran module output directory. 21 | set(CMAKE_Fortran_TARGET_MODULE_DIR "") 22 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/DependInfo.cmake: -------------------------------------------------------------------------------- 1 | # The set of languages for which implicit dependencies are needed: 2 | set(CMAKE_DEPENDS_LANGUAGES 3 | "C" 4 | ) 5 | # The set of files for implicit dependencies of each language: 6 | set(CMAKE_DEPENDS_CHECK_C 7 | "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/2.01_usual_lang_lex_re.c" "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/2.01_usual_lang_lex_re.c.o" 8 | ) 9 | set(CMAKE_C_COMPILER_ID "GNU") 10 | 11 | # The include file search paths: 12 | set(CMAKE_C_TARGET_INCLUDE_PATH 13 | ) 14 | 15 | # Targets to which this target links. 16 | set(CMAKE_TARGET_LINKED_INFO_FILES 17 | ) 18 | 19 | # Fortran module output directory. 20 | set(CMAKE_Fortran_TARGET_MODULE_DIR "") 21 | -------------------------------------------------------------------------------- /ch1/1.06/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include(${PROJECT_SOURCE_DIR}/cmake/clean_flex_bison_generate_code.cmake) 2 | 3 | execute_process(COMMAND 4 | flex -o ${CMAKE_CURRENT_BINARY_DIR}/1.06lexer.c 5 | ${CMAKE_CURRENT_SOURCE_DIR}/1.06lexer.l) 6 | 7 | execute_process(COMMAND 8 | bison -d ${CMAKE_CURRENT_SOURCE_DIR}/1.06_calculator_with_bison.y) 9 | execute_process(COMMAND 10 | mv ${CMAKE_BINARY_DIR}/1.06_calculator_with_bison.tab.c ${CMAKE_CURRENT_BINARY_DIR}) 11 | execute_process(COMMAND 12 | mv ${CMAKE_BINARY_DIR}/1.06_calculator_with_bison.tab.h ${CMAKE_CURRENT_BINARY_DIR}) 13 | 14 | add_executable(1.06 ${CMAKE_CURRENT_BINARY_DIR}/1.06lexer.c 15 | ${CMAKE_CURRENT_BINARY_DIR}/1.06_calculator_with_bison.tab.c) 16 | 17 | target_include_directories(1.06 PUBLIC 18 | ${CMAKE_BINARY_DIR}) 19 | 20 | target_link_libraries(1.06 PUBLIC 21 | -lfl) 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/DependInfo.cmake: -------------------------------------------------------------------------------- 1 | # The set of languages for which implicit dependencies are needed: 2 | set(CMAKE_DEPENDS_LANGUAGES 3 | "C" 4 | ) 5 | # The set of files for implicit dependencies of each language: 6 | set(CMAKE_DEPENDS_CHECK_C 7 | "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/1.02_BritishEn_to_AmericanEn.c" "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/1.02_BritishEn_to_AmericanEn.c.o" 8 | ) 9 | set(CMAKE_C_COMPILER_ID "GNU") 10 | 11 | # The include file search paths: 12 | set(CMAKE_C_TARGET_INCLUDE_PATH 13 | "ch1" 14 | ) 15 | 16 | # Targets to which this target links. 17 | set(CMAKE_TARGET_LINKED_INFO_FILES 18 | ) 19 | 20 | # Fortran module output directory. 21 | set(CMAKE_Fortran_TARGET_MODULE_DIR "") 22 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.06_include_file_and_start_state.dir/DependInfo.cmake: -------------------------------------------------------------------------------- 1 | # The set of languages for which implicit dependencies are needed: 2 | set(CMAKE_DEPENDS_LANGUAGES 3 | "C" 4 | ) 5 | # The set of files for implicit dependencies of each language: 6 | set(CMAKE_DEPENDS_CHECK_C 7 | "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/2.06_include_file_and_start_state.c" "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/CMakeFiles/2.06_include_file_and_start_state.dir/2.06_include_file_and_start_state.c.o" 8 | ) 9 | set(CMAKE_C_COMPILER_ID "GNU") 10 | 11 | # The include file search paths: 12 | set(CMAKE_C_TARGET_INCLUDE_PATH 13 | ) 14 | 15 | # Targets to which this target links. 16 | set(CMAKE_TARGET_LINKED_INFO_FILES 17 | ) 18 | 19 | # Fortran module output directory. 20 | set(CMAKE_Fortran_TARGET_MODULE_DIR "") 21 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.03_recognize_calculator_tokens.dir/DependInfo.cmake: -------------------------------------------------------------------------------- 1 | # The set of languages for which implicit dependencies are needed: 2 | set(CMAKE_DEPENDS_LANGUAGES 3 | "C" 4 | ) 5 | # The set of files for implicit dependencies of each language: 6 | set(CMAKE_DEPENDS_CHECK_C 7 | "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/1.03_recognize_calculator_tokens.c" "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/CMakeFiles/1.03_recognize_calculator_tokens.dir/1.03_recognize_calculator_tokens.c.o" 8 | ) 9 | set(CMAKE_C_COMPILER_ID "GNU") 10 | 11 | # The include file search paths: 12 | set(CMAKE_C_TARGET_INCLUDE_PATH 13 | "ch1" 14 | ) 15 | 16 | # Targets to which this target links. 17 | set(CMAKE_TARGET_LINKED_INFO_FILES 18 | ) 19 | 20 | # Fortran module output directory. 21 | set(CMAKE_Fortran_TARGET_MODULE_DIR "") 22 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.02_use_yyin_to_read_data_from_file.dir/DependInfo.cmake: -------------------------------------------------------------------------------- 1 | # The set of languages for which implicit dependencies are needed: 2 | set(CMAKE_DEPENDS_LANGUAGES 3 | "C" 4 | ) 5 | # The set of files for implicit dependencies of each language: 6 | set(CMAKE_DEPENDS_CHECK_C 7 | "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/2.02_use_yyin_to_read_data_from_file.c" "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/CMakeFiles/2.02_use_yyin_to_read_data_from_file.dir/2.02_use_yyin_to_read_data_from_file.c.o" 8 | ) 9 | set(CMAKE_C_COMPILER_ID "GNU") 10 | 11 | # The include file search paths: 12 | set(CMAKE_C_TARGET_INCLUDE_PATH 13 | ) 14 | 15 | # Targets to which this target links. 16 | set(CMAKE_TARGET_LINKED_INFO_FILES 17 | ) 18 | 19 | # Fortran module output directory. 20 | set(CMAKE_Fortran_TARGET_MODULE_DIR "") 21 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.02/CMakeFiles/3.02_create_AST_with_bison.dir/depend.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | ch3/3.02/CMakeFiles/3.02_create_AST_with_bison.dir/3.02_create_AST_with_bison.tab.c.o: ../ch3/3.02/fb3.02.h 5 | ch3/3.02/CMakeFiles/3.02_create_AST_with_bison.dir/3.02_create_AST_with_bison.tab.c.o: ch3/3.02/3.02_create_AST_with_bison.tab.c 6 | 7 | ch3/3.02/CMakeFiles/3.02_create_AST_with_bison.dir/3.02lexer.c.o: ../ch3/3.02/fb3.02.h 8 | ch3/3.02/CMakeFiles/3.02_create_AST_with_bison.dir/3.02lexer.c.o: ch3/3.02/3.02_create_AST_with_bison.tab.h 9 | ch3/3.02/CMakeFiles/3.02_create_AST_with_bison.dir/3.02lexer.c.o: ch3/3.02/3.02lexer.c 10 | 11 | ch3/3.02/CMakeFiles/3.02_create_AST_with_bison.dir/fb3.02.c.o: ../ch3/3.02/fb3.02.c 12 | ch3/3.02/CMakeFiles/3.02_create_AST_with_bison.dir/fb3.02.c.o: ../ch3/3.02/fb3.02.h 13 | 14 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.02/CMakeFiles/3.02_create_AST_with_bison.dir/depend.internal: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | ch3/3.02/CMakeFiles/3.02_create_AST_with_bison.dir/3.02_create_AST_with_bison.tab.c.o 5 | ../ch3/3.02/fb3.02.h 6 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.02/3.02_create_AST_with_bison.tab.c 7 | ch3/3.02/CMakeFiles/3.02_create_AST_with_bison.dir/3.02lexer.c.o 8 | ../ch3/3.02/fb3.02.h 9 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.02/3.02_create_AST_with_bison.tab.h 10 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.02/3.02lexer.c 11 | ch3/3.02/CMakeFiles/3.02_create_AST_with_bison.dir/fb3.02.c.o 12 | /home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.02/fb3.02.c 13 | /home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.02/fb3.02.h 14 | -------------------------------------------------------------------------------- /ch3/3.02/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include(${PROJECT_SOURCE_DIR}/cmake/clean_flex_bison_generate_code.cmake) 2 | 3 | 4 | # 必须预先设置好包含目录,因为在3.02_create_AST_with_bison.y文件中有包含其他头文件,Bison生成代码时会校验包含的头文件是否能够找得到 5 | include_directories(${CMAKE_CURRENT_SOURCE_DIR} 6 | ${CMAKE_CURRENT_BINARY_DIR}) 7 | 8 | execute_process(COMMAND 9 | flex -o ${CMAKE_CURRENT_BINARY_DIR}/3.02lexer.c ${CMAKE_CURRENT_SOURCE_DIR}/3.02lexer.l) 10 | 11 | execute_process(COMMAND 12 | bison -d ${CMAKE_CURRENT_SOURCE_DIR}/3.02_create_AST_with_bison.y) 13 | execute_process(COMMAND 14 | mv ${CMAKE_BINARY_DIR}/3.02_create_AST_with_bison.tab.c ${CMAKE_CURRENT_BINARY_DIR}) 15 | execute_process(COMMAND 16 | mv ${CMAKE_BINARY_DIR}/3.02_create_AST_with_bison.tab.h ${CMAKE_CURRENT_BINARY_DIR}) 17 | 18 | add_executable(3.02_create_AST_with_bison 19 | ${CMAKE_CURRENT_SOURCE_DIR}/fb3.02.c 20 | ${CMAKE_CURRENT_BINARY_DIR}/3.02lexer.c 21 | ${CMAKE_CURRENT_BINARY_DIR}/3.02_create_AST_with_bison.tab.c) 22 | 23 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/1.06/CMakeFiles/1.06.dir/C.includecache: -------------------------------------------------------------------------------- 1 | #IncludeRegexLine: ^[ ]*[#%][ ]*(include|import)[ ]*[<"]([^">]+)([">]) 2 | 3 | #IncludeRegexScan: ^.*$ 4 | 5 | #IncludeRegexComplain: ^$ 6 | 7 | #IncludeRegexTransform: 8 | 9 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/1.06/1.06_calculator_with_bison.tab.c 10 | stdio.h 11 | - 12 | stddef.h 13 | - 14 | libintl.h 15 | - 16 | alloca.h 17 | - 18 | malloc.h 19 | - 20 | stdlib.h 21 | - 22 | stdlib.h 23 | - 24 | stdio.h 25 | - 26 | 27 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/1.06/1.06_calculator_with_bison.tab.h 28 | 29 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/1.06/1.06lexer.c 30 | stdio.h 31 | - 32 | string.h 33 | - 34 | errno.h 35 | - 36 | stdlib.h 37 | - 38 | inttypes.h 39 | - 40 | 1.06_calculator_with_bison.tab.h 41 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/1.06/1.06_calculator_with_bison.tab.h 42 | unistd.h 43 | - 44 | 45 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/1.06/CMakeFiles/1.06.dir/DependInfo.cmake: -------------------------------------------------------------------------------- 1 | # The set of languages for which implicit dependencies are needed: 2 | set(CMAKE_DEPENDS_LANGUAGES 3 | "C" 4 | ) 5 | # The set of files for implicit dependencies of each language: 6 | set(CMAKE_DEPENDS_CHECK_C 7 | "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/1.06/1.06_calculator_with_bison.tab.c" "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/1.06/CMakeFiles/1.06.dir/1.06_calculator_with_bison.tab.c.o" 8 | "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/1.06/1.06lexer.c" "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/1.06/CMakeFiles/1.06.dir/1.06lexer.c.o" 9 | ) 10 | set(CMAKE_C_COMPILER_ID "GNU") 11 | 12 | # The include file search paths: 13 | set(CMAKE_C_TARGET_INCLUDE_PATH 14 | "ch1" 15 | "." 16 | ) 17 | 18 | # Targets to which this target links. 19 | set(CMAKE_TARGET_LINKED_INFO_FILES 20 | ) 21 | 22 | # Fortran module output directory. 23 | set(CMAKE_Fortran_TARGET_MODULE_DIR "") 24 | -------------------------------------------------------------------------------- /ch3/3.05/scanner.h: -------------------------------------------------------------------------------- 1 | // file: scanner.h 2 | // Created by cmp on 2020/5/11. 3 | // 4 | 5 | #ifndef FLEX_BISON_SCANNER_H 6 | #define FLEX_BISON_SCANNER_H 7 | 8 | /***********************************/ 9 | /*对yyFlexLexer进行继承,获得更多的控制 10 | * 11 | ***********************************/ 12 | 13 | /* 重要 */ 14 | #if !defined(yyFlexLexerOnce) 15 | #undef yyFlexLexer 16 | #define yyFlexLexer Marker_FlexLexer // 根据prefix修改 17 | 18 | #include 19 | 20 | #endif 21 | /* 替换默认的get_next_token定义 */ 22 | #undef YY_DECL 23 | #define YY_DECL Marker::Parser::symbol_type Marker::Scanner::nextToken() 24 | 25 | #include "parser.hpp" 26 | 27 | namespace Marker { 28 | class Driver; 29 | 30 | class Scanner : public yyFlexLexer { 31 | private: 32 | /* data */ 33 | Driver &_driver; 34 | 35 | public: 36 | Scanner(Marker::Driver &driver) : _driver(driver) {} 37 | 38 | virtual Marker::Parser::symbol_type nextToken(); // 不需要手动实现这个函数,Flex会生成YY_DECL宏定义的代码来实现这个函数 39 | 40 | virtual ~Scanner() {} 41 | }; 42 | } /* Marker */ 43 | 44 | 45 | #endif //FLEX_BISON_SCANNER_H 46 | -------------------------------------------------------------------------------- /ch1/1.05_first_calculator_with_bison.y: -------------------------------------------------------------------------------- 1 | /* 2 | // file: 1.05_first_calculator_with_bison.y 3 | // 利用Bison实现四则运算的语法解析器 4 | */ 5 | 6 | %{ 7 | #include 8 | %} 9 | 10 | /* 声明tokens记号,以便于告诉bison在语法分析程序中记号的名称。 11 | // 通常这些记号总是使用大写字母,虽然bison本身并没有这个要求。 */ 12 | %token NUMBER 13 | %token ADD SUB MUL DIV ABS 14 | %token EOL 15 | 16 | 17 | // 没有任何声明的记号的语法符号必须规则的左边出现至少一次。 18 | // 如果一个语法符号既不是声明过的记号,也没有出现在任何规则的左边, 19 | // 它将导致出现一个未声明的错误。 20 | %% 21 | calclist: /* 空规则 -- 起始符号(start symbol)有时也称为目标符号(goal symbol) */ 22 | | calclist exp EOL { printf("- %d\n", $2); } // EOL 代表一个表达式的结束。像flex一样,大括号中的表示规则的动作 23 | ; 24 | 25 | exp: factor // default $$ = $1 26 | | exp ADD factor { $$ = $1 + $3; } 27 | | exp SUB factor { $$ = $1 - $3; } 28 | ; // represent the termination of this rule. 29 | 30 | factor: term // default $$ = $1 31 | | factor MUL term { $$ = $1 * $3; } 32 | | factor DIV term { $$ = $1 / $3; } 33 | ; 34 | 35 | term: NUMBER // default $$ = $1 36 | | ABS term { $$ = $2 >= 0? $2 : - $2; } 37 | ; 38 | %% 39 | int main(int argc, char **argv) 40 | { 41 | return yyparse(); 42 | } 43 | void yyerror(char *s) 44 | { 45 | fprintf(stderr, "error: %s\n", s); 46 | } -------------------------------------------------------------------------------- /ch2/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include(${PROJECT_SOURCE_DIR}/cmake/clean_flex_bison_generate_code.cmake) 2 | 3 | execute_process(COMMAND 4 | flex -o ${CMAKE_CURRENT_BINARY_DIR}/2.01_usual_lang_lex_re.c 5 | ${CMAKE_CURRENT_SOURCE_DIR}/2.01_usual_lang_lex_re.l) 6 | 7 | add_executable(2.01_usual_lang_lex_re 8 | 2.01_usual_lang_lex_re.c) 9 | 10 | target_link_libraries(2.01_usual_lang_lex_re PUBLIC 11 | -lfl) 12 | 13 | 14 | execute_process(COMMAND 15 | flex -o ${CMAKE_CURRENT_BINARY_DIR}/2.02_use_yyin_to_read_data_from_file.c 16 | ${CMAKE_CURRENT_SOURCE_DIR}/2.02_use_yyin_to_read_data_from_file.l) 17 | 18 | add_executable(2.02_use_yyin_to_read_data_from_file 19 | 2.02_use_yyin_to_read_data_from_file.c) 20 | 21 | 22 | execute_process(COMMAND 23 | flex -o ${CMAKE_CURRENT_BINARY_DIR}/2.03_read_files.c 24 | ${CMAKE_CURRENT_SOURCE_DIR}/2.03_read_files.l) 25 | 26 | add_executable(2.03_read_files 27 | 2.03_read_files.c) 28 | 29 | 30 | execute_process(COMMAND 31 | flex -o ${CMAKE_CURRENT_BINARY_DIR}/2.06_include_file_and_start_state.c 32 | ${CMAKE_CURRENT_SOURCE_DIR}/2.06_include_file_and_start_state.l) 33 | 34 | add_executable(2.06_include_file_and_start_state 35 | 2.06_include_file_and_start_state.c) -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | cmake_minimum_required(VERSION 3.14) 3 | project(Flex_Bison) 4 | 5 | 6 | # --- 提交代码前,可以使用这些命令来清除flex和bison生成的文件 --- 7 | #execute_process(COMMAND find ${CMAKE_CURRENT_BINARY_DIR} -type f -perm -111 -name "[1-9].*" -exec rm -f {} \;) 8 | #execute_process(COMMAND find ${CMAKE_CURRENT_BINARY_DIR} -type f -name "*.h" -exec rm -f {} \;) 9 | #execute_process(COMMAND find ${CMAKE_CURRENT_BINARY_DIR} -type f -name "*.c" -exec rm -f {} \;) 10 | #execute_process(COMMAND find ${CMAKE_CURRENT_BINARY_DIR} -type f -name "*.cc" -exec rm -f {} \;) 11 | #execute_process(COMMAND find ${CMAKE_CURRENT_BINARY_DIR} -type f -name "*.hh" -exec rm -f {} \;) 12 | #execute_process(COMMAND find ${CMAKE_CURRENT_BINARY_DIR} -type f -name "*.hpp" -exec rm -f {} \;) 13 | #execute_process(COMMAND find ${CMAKE_CURRENT_BINARY_DIR} -type f -name "*.cpp" -exec rm -f {} \;) 14 | # --- 提交代码前,可以使用这些命令来清除flex和bison生成的文件 --- 15 | 16 | add_subdirectory(ch1) 17 | add_subdirectory(ch2) 18 | add_subdirectory(ch3) 19 | 20 | 21 | #include(${PROJECT_SOURCE_DIR}/cmake/clean_flex_bison_generate_code.cmake) 22 | #execute_process(COMMAND 23 | # flex -o ${CMAKE_CURRENT_BINARY_DIR}/test.c ${CMAKE_CURRENT_SOURCE_DIR}/test.l) 24 | #add_executable(test ${CMAKE_CURRENT_BINARY_DIR}/test.c) 25 | -------------------------------------------------------------------------------- /ch2/2.02_use_yyin_to_read_data_from_file.l: -------------------------------------------------------------------------------- 1 | /* 2 | // file: 2.02_use_yyin_to_read_data_from_file.y 3 | // flex词法分析器默认从stdin读取输入 4 | // 5 | // 除非你对yyin另做安排,词法分析器总是通过yyin文件描述符读取输入,因此为了让flex从文件读取输入 6 | // 你只需要在第一次调用yylex之前重新设定yyin文件描述符 7 | // */ 8 | 9 | /* 10 | // flex和lex总是提供了一个小型的库,叫做-lfl,它定义了默认的main例程,同时也定义了早期lex遗留至今的鸡肋——默认版本的yywrap 11 | // flex的较新的版本允许你在词法分析器的开头设定%option noyywrap,来要求不使用yywrap 12 | // lfl库中的main函数,如下: 13 | // int main() 14 | // { 15 | // while (yylex() != 0); 16 | // return 0; 17 | // } 18 | // 如果你在flex程序使用%option noyywrap,并且定义了自己的main函数,你就不需要链接-lfl了。 19 | // */ 20 | %option noyywrap 21 | %{ 22 | int chars = 0; 23 | int words = 0; 24 | int lines = 0; 25 | %} 26 | 27 | %% 28 | [a-zA-Z]+ { words++; chars += strlen(yytext); } 29 | \n { chars++; lines++; } 30 | . { chars++;} 31 | 32 | %% 33 | 34 | int main(int argc, char ** argv) 35 | { 36 | if ( argc > 1 ) { 37 | if (! (yyin = fopen(argv[1], "r" ) )){ 38 | perror(argv[1]); 39 | return 1; 40 | } 41 | printf("[I]>>>Read data from file:%s\n", argv[1]); 42 | }else{ 43 | printf("[I]>>>Read data from stdin.\n"); 44 | } 45 | yylex(); 46 | printf("chars=%8d\n", chars ); 47 | printf("words=%8d\n", words ); 48 | printf("lines=%8d\n", lines ); 49 | return 0; 50 | } -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.02/CMakeFiles/3.02_create_AST_with_bison.dir/C.includecache: -------------------------------------------------------------------------------- 1 | #IncludeRegexLine: ^[ ]*[#%][ ]*(include|import)[ ]*[<"]([^">]+)([">]) 2 | 3 | #IncludeRegexScan: ^.*$ 4 | 5 | #IncludeRegexComplain: ^$ 6 | 7 | #IncludeRegexTransform: 8 | 9 | ../ch3/3.02/fb3.02.h 10 | 11 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.02/3.02_create_AST_with_bison.tab.c 12 | stdio.h 13 | - 14 | stdlib.h 15 | - 16 | fb3.02.h 17 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.02/fb3.02.h 18 | stddef.h 19 | - 20 | libintl.h 21 | - 22 | alloca.h 23 | - 24 | malloc.h 25 | - 26 | stdlib.h 27 | - 28 | stdlib.h 29 | - 30 | stdio.h 31 | - 32 | 33 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.02/3.02_create_AST_with_bison.tab.h 34 | 35 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.02/3.02lexer.c 36 | stdio.h 37 | - 38 | string.h 39 | - 40 | errno.h 41 | - 42 | stdlib.h 43 | - 44 | inttypes.h 45 | - 46 | fb3.02.h 47 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.02/fb3.02.h 48 | 3.02_create_AST_with_bison.tab.h 49 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.02/3.02_create_AST_with_bison.tab.h 50 | unistd.h 51 | - 52 | 53 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/cmake_install.cmake: -------------------------------------------------------------------------------- 1 | # Install script for directory: /home/cmp/work_dir/source_code/yacc_bison_practice/ch2 2 | 3 | # Set the install prefix 4 | if(NOT DEFINED CMAKE_INSTALL_PREFIX) 5 | set(CMAKE_INSTALL_PREFIX "/usr/local") 6 | endif() 7 | string(REGEX REPLACE "/$" "" CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}") 8 | 9 | # Set the install configuration name. 10 | if(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME) 11 | if(BUILD_TYPE) 12 | string(REGEX REPLACE "^[^A-Za-z0-9_]+" "" 13 | CMAKE_INSTALL_CONFIG_NAME "${BUILD_TYPE}") 14 | else() 15 | set(CMAKE_INSTALL_CONFIG_NAME "Debug") 16 | endif() 17 | message(STATUS "Install configuration: \"${CMAKE_INSTALL_CONFIG_NAME}\"") 18 | endif() 19 | 20 | # Set the component getting installed. 21 | if(NOT CMAKE_INSTALL_COMPONENT) 22 | if(COMPONENT) 23 | message(STATUS "Install component: \"${COMPONENT}\"") 24 | set(CMAKE_INSTALL_COMPONENT "${COMPONENT}") 25 | else() 26 | set(CMAKE_INSTALL_COMPONENT) 27 | endif() 28 | endif() 29 | 30 | # Install shared libraries without execute permission? 31 | if(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE) 32 | set(CMAKE_INSTALL_SO_NO_EXE "1") 33 | endif() 34 | 35 | # Is this installation the result of a crosscompile? 36 | if(NOT DEFINED CMAKE_CROSSCOMPILING) 37 | set(CMAKE_CROSSCOMPILING "FALSE") 38 | endif() 39 | 40 | -------------------------------------------------------------------------------- /cmake-build-debug/ch5/cmake_install.cmake: -------------------------------------------------------------------------------- 1 | # Install script for directory: /home/cmp/work_dir/source_code/yacc_bison_practice/ch5 2 | 3 | # Set the install prefix 4 | if(NOT DEFINED CMAKE_INSTALL_PREFIX) 5 | set(CMAKE_INSTALL_PREFIX "/usr/local") 6 | endif() 7 | string(REGEX REPLACE "/$" "" CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}") 8 | 9 | # Set the install configuration name. 10 | if(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME) 11 | if(BUILD_TYPE) 12 | string(REGEX REPLACE "^[^A-Za-z0-9_]+" "" 13 | CMAKE_INSTALL_CONFIG_NAME "${BUILD_TYPE}") 14 | else() 15 | set(CMAKE_INSTALL_CONFIG_NAME "Debug") 16 | endif() 17 | message(STATUS "Install configuration: \"${CMAKE_INSTALL_CONFIG_NAME}\"") 18 | endif() 19 | 20 | # Set the component getting installed. 21 | if(NOT CMAKE_INSTALL_COMPONENT) 22 | if(COMPONENT) 23 | message(STATUS "Install component: \"${COMPONENT}\"") 24 | set(CMAKE_INSTALL_COMPONENT "${COMPONENT}") 25 | else() 26 | set(CMAKE_INSTALL_COMPONENT) 27 | endif() 28 | endif() 29 | 30 | # Install shared libraries without execute permission? 31 | if(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE) 32 | set(CMAKE_INSTALL_SO_NO_EXE "1") 33 | endif() 34 | 35 | # Is this installation the result of a crosscompile? 36 | if(NOT DEFINED CMAKE_CROSSCOMPILING) 37 | set(CMAKE_CROSSCOMPILING "FALSE") 38 | endif() 39 | 40 | -------------------------------------------------------------------------------- /ch3/3.02/3.02_create_AST_with_bison.y: -------------------------------------------------------------------------------- 1 | /* file: 3.02_create_AST_with_bison.y */ 2 | 3 | %{ 4 | #include 5 | #include 6 | #include "fb3.02.h" 7 | %} 8 | 9 | /* %union关键字声明了语法规则中所有语法值可能会用到的数据类型的一个集合 */ 10 | %union { 11 | struct ASTNode *a; 12 | double d; 13 | } 14 | 15 | %token NUMBER /* 声明记号,而""指定了该记号的数据类型 */ 16 | %token EOL 17 | 18 | /* 当使用了union声明了一组数据类型集合后,你必须为所有的非终结符指定其值类型。使用"%type"关键字*/ 19 | %type exp factor term 20 | 21 | %start calclist /* 指定起始符号(start symbol)有时也称为目标符号(goal symbol) */ 22 | 23 | %% 24 | calclist: /* -- 空规则,起始符号必须具备一个空规则,旨在让起始符号必须匹配整个输入 -- */ 25 | | calclist exp EOL { printf("= %4.4g\n", eval($2)); treefree($2); printf("> "); } 26 | | calclist EOL { printf("> "); } /* 空行或注释 */ 27 | ; 28 | 29 | exp: factor 30 | | exp '+' factor { $$ = newast(NT_ADD, $1, $3); } 31 | | exp '-' factor { $$ = newast(NT_SUB, $1, $3); } 32 | ; 33 | 34 | factor: term 35 | | factor '*' term { $$ = newast(NT_MUL, $1, $3); } 36 | | factor '/' term { $$ = newast(NT_DIV, $1, $3); } 37 | ; 38 | 39 | term: NUMBER { $$ = newnum($1); } 40 | | '|' exp '|' { $$ = newast(NT_ABS, $2, NULL); } // 使用绝对值符号的语法规则 41 | | '(' exp ')' { $$ = $2; } // 使用圆括号的语法规则 42 | | '-' term { $$ = newast(NT_NEG, $2, NULL); } // 使用负号的规则 43 | ; 44 | %% 45 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/1.06/cmake_install.cmake: -------------------------------------------------------------------------------- 1 | # Install script for directory: /home/cmp/work_dir/source_code/yacc_bison_practice/ch1/1.06 2 | 3 | # Set the install prefix 4 | if(NOT DEFINED CMAKE_INSTALL_PREFIX) 5 | set(CMAKE_INSTALL_PREFIX "/usr/local") 6 | endif() 7 | string(REGEX REPLACE "/$" "" CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}") 8 | 9 | # Set the install configuration name. 10 | if(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME) 11 | if(BUILD_TYPE) 12 | string(REGEX REPLACE "^[^A-Za-z0-9_]+" "" 13 | CMAKE_INSTALL_CONFIG_NAME "${BUILD_TYPE}") 14 | else() 15 | set(CMAKE_INSTALL_CONFIG_NAME "Debug") 16 | endif() 17 | message(STATUS "Install configuration: \"${CMAKE_INSTALL_CONFIG_NAME}\"") 18 | endif() 19 | 20 | # Set the component getting installed. 21 | if(NOT CMAKE_INSTALL_COMPONENT) 22 | if(COMPONENT) 23 | message(STATUS "Install component: \"${COMPONENT}\"") 24 | set(CMAKE_INSTALL_COMPONENT "${COMPONENT}") 25 | else() 26 | set(CMAKE_INSTALL_COMPONENT) 27 | endif() 28 | endif() 29 | 30 | # Install shared libraries without execute permission? 31 | if(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE) 32 | set(CMAKE_INSTALL_SO_NO_EXE "1") 33 | endif() 34 | 35 | # Is this installation the result of a crosscompile? 36 | if(NOT DEFINED CMAKE_CROSSCOMPILING) 37 | set(CMAKE_CROSSCOMPILING "FALSE") 38 | endif() 39 | 40 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.02/CMakeFiles/3.02_create_AST_with_bison.dir/DependInfo.cmake: -------------------------------------------------------------------------------- 1 | # The set of languages for which implicit dependencies are needed: 2 | set(CMAKE_DEPENDS_LANGUAGES 3 | "C" 4 | ) 5 | # The set of files for implicit dependencies of each language: 6 | set(CMAKE_DEPENDS_CHECK_C 7 | "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.02/3.02_create_AST_with_bison.tab.c" "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.02/CMakeFiles/3.02_create_AST_with_bison.dir/3.02_create_AST_with_bison.tab.c.o" 8 | "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.02/3.02lexer.c" "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.02/CMakeFiles/3.02_create_AST_with_bison.dir/3.02lexer.c.o" 9 | "/home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.02/fb3.02.c" "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.02/CMakeFiles/3.02_create_AST_with_bison.dir/fb3.02.c.o" 10 | ) 11 | set(CMAKE_C_COMPILER_ID "GNU") 12 | 13 | # The include file search paths: 14 | set(CMAKE_C_TARGET_INCLUDE_PATH 15 | "../ch3/3.02" 16 | "ch3/3.02" 17 | ) 18 | 19 | # Targets to which this target links. 20 | set(CMAKE_TARGET_LINKED_INFO_FILES 21 | ) 22 | 23 | # Fortran module output directory. 24 | set(CMAKE_Fortran_TARGET_MODULE_DIR "") 25 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.02/cmake_install.cmake: -------------------------------------------------------------------------------- 1 | # Install script for directory: /home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.02 2 | 3 | # Set the install prefix 4 | if(NOT DEFINED CMAKE_INSTALL_PREFIX) 5 | set(CMAKE_INSTALL_PREFIX "/usr/local") 6 | endif() 7 | string(REGEX REPLACE "/$" "" CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}") 8 | 9 | # Set the install configuration name. 10 | if(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME) 11 | if(BUILD_TYPE) 12 | string(REGEX REPLACE "^[^A-Za-z0-9_]+" "" 13 | CMAKE_INSTALL_CONFIG_NAME "${BUILD_TYPE}") 14 | else() 15 | set(CMAKE_INSTALL_CONFIG_NAME "Debug") 16 | endif() 17 | message(STATUS "Install configuration: \"${CMAKE_INSTALL_CONFIG_NAME}\"") 18 | endif() 19 | 20 | # Set the component getting installed. 21 | if(NOT CMAKE_INSTALL_COMPONENT) 22 | if(COMPONENT) 23 | message(STATUS "Install component: \"${COMPONENT}\"") 24 | set(CMAKE_INSTALL_COMPONENT "${COMPONENT}") 25 | else() 26 | set(CMAKE_INSTALL_COMPONENT) 27 | endif() 28 | endif() 29 | 30 | # Install shared libraries without execute permission? 31 | if(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE) 32 | set(CMAKE_INSTALL_SO_NO_EXE "1") 33 | endif() 34 | 35 | # Is this installation the result of a crosscompile? 36 | if(NOT DEFINED CMAKE_CROSSCOMPILING) 37 | set(CMAKE_CROSSCOMPILING "FALSE") 38 | endif() 39 | 40 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.05/cmake_install.cmake: -------------------------------------------------------------------------------- 1 | # Install script for directory: /home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.05 2 | 3 | # Set the install prefix 4 | if(NOT DEFINED CMAKE_INSTALL_PREFIX) 5 | set(CMAKE_INSTALL_PREFIX "/usr/local") 6 | endif() 7 | string(REGEX REPLACE "/$" "" CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}") 8 | 9 | # Set the install configuration name. 10 | if(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME) 11 | if(BUILD_TYPE) 12 | string(REGEX REPLACE "^[^A-Za-z0-9_]+" "" 13 | CMAKE_INSTALL_CONFIG_NAME "${BUILD_TYPE}") 14 | else() 15 | set(CMAKE_INSTALL_CONFIG_NAME "Debug") 16 | endif() 17 | message(STATUS "Install configuration: \"${CMAKE_INSTALL_CONFIG_NAME}\"") 18 | endif() 19 | 20 | # Set the component getting installed. 21 | if(NOT CMAKE_INSTALL_COMPONENT) 22 | if(COMPONENT) 23 | message(STATUS "Install component: \"${COMPONENT}\"") 24 | set(CMAKE_INSTALL_COMPONENT "${COMPONENT}") 25 | else() 26 | set(CMAKE_INSTALL_COMPONENT) 27 | endif() 28 | endif() 29 | 30 | # Install shared libraries without execute permission? 31 | if(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE) 32 | set(CMAKE_INSTALL_SO_NO_EXE "1") 33 | endif() 34 | 35 | # Is this installation the result of a crosscompile? 36 | if(NOT DEFINED CMAKE_CROSSCOMPILING) 37 | set(CMAKE_CROSSCOMPILING "FALSE") 38 | endif() 39 | 40 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.05/CMakeFiles/3.05.dir/DependInfo.cmake: -------------------------------------------------------------------------------- 1 | # The set of languages for which implicit dependencies are needed: 2 | set(CMAKE_DEPENDS_LANGUAGES 3 | "CXX" 4 | ) 5 | # The set of files for implicit dependencies of each language: 6 | set(CMAKE_DEPENDS_CHECK_CXX 7 | "/home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.05/driver.cpp" "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/CMakeFiles/3.05.dir/driver.cpp.o" 8 | "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/lexer.cpp" "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/CMakeFiles/3.05.dir/lexer.cpp.o" 9 | "/home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.05/main.cpp" "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/CMakeFiles/3.05.dir/main.cpp.o" 10 | "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/parser.cpp" "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/CMakeFiles/3.05.dir/parser.cpp.o" 11 | ) 12 | set(CMAKE_CXX_COMPILER_ID "GNU") 13 | 14 | # The include file search paths: 15 | set(CMAKE_CXX_TARGET_INCLUDE_PATH 16 | "../ch3/3.05" 17 | "ch3/3.05" 18 | ) 19 | 20 | # Targets to which this target links. 21 | set(CMAKE_TARGET_LINKED_INFO_FILES 22 | ) 23 | 24 | # Fortran module output directory. 25 | set(CMAKE_Fortran_TARGET_MODULE_DIR "") 26 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.05/CMakeFiles/3.05_bak.dir/DependInfo.cmake: -------------------------------------------------------------------------------- 1 | # The set of languages for which implicit dependencies are needed: 2 | set(CMAKE_DEPENDS_LANGUAGES 3 | "CXX" 4 | ) 5 | # The set of files for implicit dependencies of each language: 6 | set(CMAKE_DEPENDS_CHECK_CXX 7 | "/home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.05/driver.cpp" "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/CMakeFiles/3.05_bak.dir/driver.cpp.o" 8 | "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/lexer.cpp" "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/CMakeFiles/3.05_bak.dir/lexer.cpp.o" 9 | "/home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.05/main.cpp" "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/CMakeFiles/3.05_bak.dir/main.cpp.o" 10 | "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/parser.cpp" "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/CMakeFiles/3.05_bak.dir/parser.cpp.o" 11 | ) 12 | set(CMAKE_CXX_COMPILER_ID "GNU") 13 | 14 | # The include file search paths: 15 | set(CMAKE_CXX_TARGET_INCLUDE_PATH 16 | "../ch3/3.05" 17 | "ch3/3.05" 18 | ) 19 | 20 | # Targets to which this target links. 21 | set(CMAKE_TARGET_LINKED_INFO_FILES 22 | ) 23 | 24 | # Fortran module output directory. 25 | set(CMAKE_Fortran_TARGET_MODULE_DIR "") 26 | -------------------------------------------------------------------------------- /ch2/2.03_read_files.l: -------------------------------------------------------------------------------- 1 | /* 2 | // file: 2.03_read_files.y 3 | // 使用flex读取多个文件作为输入 4 | // 5 | // */ 6 | 7 | %option noyywrap 8 | %{ 9 | int chars = 0; 10 | int words = 0; 11 | int lines = 0; 12 | 13 | int totchars = 0; 14 | int totwords = 0; 15 | int totlines = 0; 16 | %} 17 | 18 | %% 19 | [a-zA-Z]+ { words++; chars += strlen(yytext); } 20 | \n { chars++; lines++; } 21 | . { chars++; } 22 | %% 23 | 24 | #define HEADLINE "file chars words lines \n" 25 | #define STREAMLINE "%-20s%-8d%-8d%-8d\n" 26 | 27 | int main(int argc, char ** argv) 28 | { 29 | int i; 30 | if ( argc < 2 ){ // 读取标准输入 31 | yylex(); 32 | printf(HEADLINE); 33 | printf(STREAMLINE, "STDIN", chars, words, lines); 34 | return 0; 35 | } 36 | printf(HEADLINE); 37 | for(i = 1; i < argc; i++){ 38 | FILE *f = fopen(argv[i], "r"); 39 | if (!f){ 40 | perror(argv[i]); 41 | return 1; 42 | } 43 | // 每打开一个文件,使用yyrestart()函数把打开的文件描述符指定给词法分析器, 44 | // 词法分析器将使用当前指定的文件描述符作为新的输入读取,然后再调用yylex()函数进行词法分析。 45 | yyrestart(f); 46 | yylex(); 47 | fclose(f); 48 | printf(STREAMLINE, argv[i], chars, words, lines); // 打印每个文件的统计信息 49 | totchars += chars; chars = 0; 50 | totwords += words; words = 0; 51 | totlines += lines; lines = 0; 52 | } 53 | // 打印总共的统计信息 54 | printf("\nTotal:\n"); 55 | printf("chars words lines \n"); 56 | printf("%-8d%-8d%-8d\n", totchars, totwords, totlines); 57 | return 0; 58 | } -------------------------------------------------------------------------------- /cmake-build-debug/ch1/cmake_install.cmake: -------------------------------------------------------------------------------- 1 | # Install script for directory: /home/cmp/work_dir/source_code/yacc_bison_practice/ch1 2 | 3 | # Set the install prefix 4 | if(NOT DEFINED CMAKE_INSTALL_PREFIX) 5 | set(CMAKE_INSTALL_PREFIX "/usr/local") 6 | endif() 7 | string(REGEX REPLACE "/$" "" CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}") 8 | 9 | # Set the install configuration name. 10 | if(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME) 11 | if(BUILD_TYPE) 12 | string(REGEX REPLACE "^[^A-Za-z0-9_]+" "" 13 | CMAKE_INSTALL_CONFIG_NAME "${BUILD_TYPE}") 14 | else() 15 | set(CMAKE_INSTALL_CONFIG_NAME "Debug") 16 | endif() 17 | message(STATUS "Install configuration: \"${CMAKE_INSTALL_CONFIG_NAME}\"") 18 | endif() 19 | 20 | # Set the component getting installed. 21 | if(NOT CMAKE_INSTALL_COMPONENT) 22 | if(COMPONENT) 23 | message(STATUS "Install component: \"${COMPONENT}\"") 24 | set(CMAKE_INSTALL_COMPONENT "${COMPONENT}") 25 | else() 26 | set(CMAKE_INSTALL_COMPONENT) 27 | endif() 28 | endif() 29 | 30 | # Install shared libraries without execute permission? 31 | if(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE) 32 | set(CMAKE_INSTALL_SO_NO_EXE "1") 33 | endif() 34 | 35 | # Is this installation the result of a crosscompile? 36 | if(NOT DEFINED CMAKE_CROSSCOMPILING) 37 | set(CMAKE_CROSSCOMPILING "FALSE") 38 | endif() 39 | 40 | if(NOT CMAKE_INSTALL_LOCAL_ONLY) 41 | # Include the install script for the subdirectory. 42 | include("/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/1.06/cmake_install.cmake") 43 | endif() 44 | 45 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.05/CMakeFiles/3.05.dir/C.includecache: -------------------------------------------------------------------------------- 1 | #IncludeRegexLine: ^[ ]*[#%][ ]*(include|import)[ ]*[<"]([^">]+)([">]) 2 | 3 | #IncludeRegexScan: ^.*$ 4 | 5 | #IncludeRegexComplain: ^$ 6 | 7 | #IncludeRegexTransform: 8 | 9 | ../ch3/3.05/cppcalc.ctx.h 10 | 11 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/cppcalc.c 12 | stdio.h 13 | - 14 | string.h 15 | - 16 | errno.h 17 | - 18 | stdlib.h 19 | - 20 | inttypes.h 21 | - 22 | stdlib.h 23 | - 24 | cppcalc.ctx.h 25 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/cppcalc.ctx.h 26 | cppcalc.tab.hh 27 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/cppcalc.tab.hh 28 | unistd.h 29 | - 30 | 31 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/cppcalc.tab.hh 32 | cstdlib 33 | - 34 | iostream 35 | - 36 | stdexcept 37 | - 38 | string 39 | - 40 | vector 41 | - 42 | stack.hh 43 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/stack.hh 44 | location.hh 45 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/location.hh 46 | 47 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/location.hh 48 | position.hh 49 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/position.hh 50 | 51 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/position.hh 52 | algorithm 53 | - 54 | iostream 55 | - 56 | string 57 | - 58 | 59 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/stack.hh 60 | vector 61 | - 62 | 63 | -------------------------------------------------------------------------------- /ch1/1.01_word_counter.l: -------------------------------------------------------------------------------- 1 | /* file: 1.01_word_counter.y */ 2 | /* 一个字数统计(word_counter)程序,程序读入一个文件然后报告这个文件的行数、单词树和字符数。*/ 3 | /* 以下是Flex的第一部分为定义部分,包含选项、文字块、起始条件和转换等。 */ 4 | /* 文字块存在与%{和%}之间,它们将被原样拷贝到生成文件中。 */ 5 | /* 以空白字符开头的行会被原样拷贝到C文件中。通常用于"\/**\/"之间的多行注释,在每行前面需要有空白字符。 */ 6 | /* 例如:用于声明选项(%option),以及起始条件start condition(使用%x、%s声明) */ 7 | %option noyywrap 8 | 9 | /* %{ 和 %}之间的代码被认为是文字块,会被原样拷贝到生成的文件的开头部分。*/ 10 | %{ 11 | int chars = 0; // 定义变量,用于计数有多少个字符 12 | int words = 0; // 定义变量,用于计数有多少个单词 13 | int lines = 0; // 定义变量,用于计数有多少行 14 | %} 15 | 16 | 17 | /* 以下%%和%%之间是Flex的第二部分为规则部分,该部分定义一些匹配模式(每个模式必须放在行首)和动作(模式匹配成功是所执行的C代码,使用{}括号括住的一行或多行语句)。 18 | 注意: 19 | 在该部分也有%{和%}定义的C代码语句,它们会被原封不动地拷贝到yylex()中。 20 | 在规则部分开头出现的C代码也会出现在yylex的开头,它可以包含词法分析器使用的变量,以及每次yylex()调用时所需运行的代码。 21 | 在其他部分出现的C代码必须仅仅包含注释,因为你无法预知它出现在词法分析器的什么位置。*/ 22 | %% 23 | [a-zA-Z]+ { 24 | words++; 25 | chars += strlen(yytext); // yytext变量总是指向本次匹配的输入文本字符串 26 | } // 当满足正则表达式:[a-zA-Z]+,表示成功识别到了一个单词。words++并且chars增加单词的字符长度 27 | \n {chars++; lines++; } // 当满足正则表达式:\n,表示识别到了换行符。chars++并且lines++ 28 | . {chars++; } // 当满足正则表达式:.,表示识别到了任意一个单词。chars++ 29 | %% 30 | /* 以下是Flex的第三部分为用户子例程部分,该部分会被拷贝到生成的词法分析器里面的C代码。 31 | 它们通常是一些与动作相关的程序代码。 32 | */ 33 | int main(int argc, char **argv) 34 | { 35 | yylex(); // 该函数是词法分析器的主要函数,调用它将进行词法分析 36 | printf("chars=%8d\n",chars); // 打印有多少个字符 37 | printf("words=%8d\n",words); // 打印有多少个单词 38 | printf("lines=%8d\n",lines); // 打印有多少行 39 | return 0; 40 | } 41 | 42 | /* 43 | 注意: 44 | Q:上面的例子可以发现,第三条规则会匹配所有的字符, 45 | 难道它不会也匹配第一个模式所能匹配的单词字母吗? 46 | A:如果两个模式都匹配的话,flex会选择在程序里面首先出现的那个模式。 47 | */ -------------------------------------------------------------------------------- /ch1/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include(${PROJECT_SOURCE_DIR}/cmake/clean_flex_bison_generate_code.cmake) 2 | 3 | 4 | execute_process(COMMAND 5 | flex -o ${CMAKE_CURRENT_BINARY_DIR}/1.01_word_counter.c ${CMAKE_CURRENT_SOURCE_DIR}/1.01_word_counter.l) 6 | add_executable(1.01_word_counter ${CMAKE_CURRENT_BINARY_DIR}/1.01_word_counter.c) 7 | target_link_libraries(1.01_word_counter PUBLIC 8 | -lfl) 9 | 10 | execute_process(COMMAND 11 | flex -o ${CMAKE_CURRENT_BINARY_DIR}/1.02_BritishEn_to_AmericanEn.c ${CMAKE_CURRENT_SOURCE_DIR}/1.02_BritishEn_to_AmericanEn.l) 12 | add_executable(1.02_BritishEn_to_AmericanEn ${CMAKE_CURRENT_BINARY_DIR}/1.02_BritishEn_to_AmericanEn.c) 13 | target_link_libraries(1.02_BritishEn_to_AmericanEn PUBLIC 14 | -lfl) 15 | 16 | execute_process(COMMAND 17 | flex -o ${CMAKE_CURRENT_BINARY_DIR}/1.03_recognize_calculator_tokens.c ${CMAKE_CURRENT_SOURCE_DIR}/1.03_recognize_calculator_tokens.l) 18 | add_executable(1.03_recognize_calculator_tokens ${CMAKE_CURRENT_BINARY_DIR}/1.03_recognize_calculator_tokens.c) 19 | target_link_libraries(1.03_recognize_calculator_tokens PUBLIC 20 | -lfl) 21 | 22 | include_directories(${CMAKE_CURRENT_BINARY_DIR}/) 23 | execute_process(COMMAND 24 | flex -o ${CMAKE_CURRENT_BINARY_DIR}/1.04_NextToken_with_Flex.h ${CMAKE_CURRENT_SOURCE_DIR}/1.04_NextToken_with_Flex.l) 25 | add_executable(1.04_NextToken_with_Flex 1.04_main.c) 26 | target_link_libraries(1.04_NextToken_with_Flex PUBLIC 27 | -lfl) 28 | 29 | 30 | execute_process(COMMAND 31 | bison -o ${CMAKE_CURRENT_BINARY_DIR}/1.05_first_calculator_with_bison.c ${CMAKE_CURRENT_SOURCE_DIR}/1.05_first_calculator_with_bison.y) 32 | 33 | 34 | add_subdirectory(1.06) 35 | -------------------------------------------------------------------------------- /ch1/1.06/1.06_calculator_with_bison.y: -------------------------------------------------------------------------------- 1 | /* 2 | // file: 1.05_first_calculator_with_bison.y 3 | // 使用Flex和Bison手写词法分析器和语法分析器,实现一个简单的计算器 4 | // 这是语法解析器部分 5 | */ 6 | 7 | %{ 8 | #include 9 | %} 10 | 11 | /* 声明tokens记号,以便于告诉bison在语法分析程序中记号的名称。 12 | // 通常这些记号总是使用大写字母,虽然bison本身并没有这个要求。 */ 13 | %token NUMBER 14 | %token ADD SUB MUL DIV ABS 15 | %token L_PARENTHESIS R_PARENTHESIS 16 | %token EOL 17 | 18 | %start calclist /* 指定起始符号(start symbol)有时也称为目标符号(goal symbol) */ 19 | 20 | // 下面%%之后的是bison的语法规则部分,语法规则使用的是BNF文法。BNF文法是上下文无关文法(Context-Free Grammar,CFG)的标准格式。 21 | // 注意: 22 | // 没有任何声明的记号的语法符号必须规则的左边出现至少一次。如果一个语法符号既不是声明过的记号,也没有出现在任何规则的左边,它将导致出现一个未声明的错误。 23 | // 24 | %% 25 | calclist: /* -- 空规则,起始符号必须具备一个空规则,旨在让起始符号必须匹配整个输入 -- */ 26 | | calclist exp EOL { printf("= %d\n", $2); } // EOL 代表一个表达式的结束。像flex一样,大括号中的表示规则的动作 27 | ; 28 | 29 | exp: exp ADD factor { $$ = $1 + $3; } // 加法语法规则 30 | | exp SUB factor { $$ = $1 - $3; } // 减法语法规则 31 | | factor 32 | ; // represent the termination of this rule. 33 | 34 | factor: factor MUL term { $$ = $1 * $3; } // 乘法语法规则 35 | | factor DIV term { $$ = $1 / $3; } // 除法语法规则 36 | | term 37 | ; 38 | 39 | term: NUMBER 40 | | ABS exp ABS { $$ = $2 >= 0? $2 : - $2; } // 增加使用绝对值符号的语法规则 41 | | L_PARENTHESIS exp R_PARENTHESIS { $$ = $2; } // 增加使用圆括号的语法规则 42 | ; 43 | %% 44 | int main(int argc, char **argv) 45 | { 46 | // yyparse是语法解析函数,调用yyparse将开始语法解析。 47 | // 该函数读取tokens并语法规则匹配,匹配成功后,执行相应的动作,直到文件的读取结束或者发生不可恢复的错误函数将返回。 48 | // 此外,你也可以在action中使yyparse直接提前返回。 49 | yyparse(); 50 | return 0; 51 | } 52 | int yyerror(char *s) 53 | { 54 | return fprintf(stderr, "error: %s\n", s); 55 | } -------------------------------------------------------------------------------- /cmake-build-debug/ch3/cmake_install.cmake: -------------------------------------------------------------------------------- 1 | # Install script for directory: /home/cmp/work_dir/source_code/yacc_bison_practice/ch3 2 | 3 | # Set the install prefix 4 | if(NOT DEFINED CMAKE_INSTALL_PREFIX) 5 | set(CMAKE_INSTALL_PREFIX "/usr/local") 6 | endif() 7 | string(REGEX REPLACE "/$" "" CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}") 8 | 9 | # Set the install configuration name. 10 | if(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME) 11 | if(BUILD_TYPE) 12 | string(REGEX REPLACE "^[^A-Za-z0-9_]+" "" 13 | CMAKE_INSTALL_CONFIG_NAME "${BUILD_TYPE}") 14 | else() 15 | set(CMAKE_INSTALL_CONFIG_NAME "Debug") 16 | endif() 17 | message(STATUS "Install configuration: \"${CMAKE_INSTALL_CONFIG_NAME}\"") 18 | endif() 19 | 20 | # Set the component getting installed. 21 | if(NOT CMAKE_INSTALL_COMPONENT) 22 | if(COMPONENT) 23 | message(STATUS "Install component: \"${COMPONENT}\"") 24 | set(CMAKE_INSTALL_COMPONENT "${COMPONENT}") 25 | else() 26 | set(CMAKE_INSTALL_COMPONENT) 27 | endif() 28 | endif() 29 | 30 | # Install shared libraries without execute permission? 31 | if(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE) 32 | set(CMAKE_INSTALL_SO_NO_EXE "1") 33 | endif() 34 | 35 | # Is this installation the result of a crosscompile? 36 | if(NOT DEFINED CMAKE_CROSSCOMPILING) 37 | set(CMAKE_CROSSCOMPILING "FALSE") 38 | endif() 39 | 40 | if(NOT CMAKE_INSTALL_LOCAL_ONLY) 41 | # Include the install script for the subdirectory. 42 | include("/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.02/cmake_install.cmake") 43 | endif() 44 | 45 | if(NOT CMAKE_INSTALL_LOCAL_ONLY) 46 | # Include the install script for the subdirectory. 47 | include("/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/cmake_install.cmake") 48 | endif() 49 | 50 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.05/CMakeFiles/3.05.dir/depend.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | ch3/3.05/CMakeFiles/3.05.dir/driver.cpp.o: ../ch3/3.05/driver.cpp 5 | ch3/3.05/CMakeFiles/3.05.dir/driver.cpp.o: ../ch3/3.05/driver.h 6 | ch3/3.05/CMakeFiles/3.05.dir/driver.cpp.o: ../ch3/3.05/scanner.h 7 | ch3/3.05/CMakeFiles/3.05.dir/driver.cpp.o: ch3/3.05/location.hh 8 | ch3/3.05/CMakeFiles/3.05.dir/driver.cpp.o: ch3/3.05/parser.hpp 9 | ch3/3.05/CMakeFiles/3.05.dir/driver.cpp.o: ch3/3.05/position.hh 10 | ch3/3.05/CMakeFiles/3.05.dir/driver.cpp.o: ch3/3.05/stack.hh 11 | 12 | ch3/3.05/CMakeFiles/3.05.dir/lexer.cpp.o: ../ch3/3.05/scanner.h 13 | ch3/3.05/CMakeFiles/3.05.dir/lexer.cpp.o: ch3/3.05/lexer.cpp 14 | ch3/3.05/CMakeFiles/3.05.dir/lexer.cpp.o: ch3/3.05/location.hh 15 | ch3/3.05/CMakeFiles/3.05.dir/lexer.cpp.o: ch3/3.05/parser.hpp 16 | ch3/3.05/CMakeFiles/3.05.dir/lexer.cpp.o: ch3/3.05/position.hh 17 | ch3/3.05/CMakeFiles/3.05.dir/lexer.cpp.o: ch3/3.05/stack.hh 18 | 19 | ch3/3.05/CMakeFiles/3.05.dir/main.cpp.o: ../ch3/3.05/driver.h 20 | ch3/3.05/CMakeFiles/3.05.dir/main.cpp.o: ../ch3/3.05/main.cpp 21 | ch3/3.05/CMakeFiles/3.05.dir/main.cpp.o: ../ch3/3.05/scanner.h 22 | ch3/3.05/CMakeFiles/3.05.dir/main.cpp.o: ch3/3.05/location.hh 23 | ch3/3.05/CMakeFiles/3.05.dir/main.cpp.o: ch3/3.05/parser.hpp 24 | ch3/3.05/CMakeFiles/3.05.dir/main.cpp.o: ch3/3.05/position.hh 25 | ch3/3.05/CMakeFiles/3.05.dir/main.cpp.o: ch3/3.05/stack.hh 26 | 27 | ch3/3.05/CMakeFiles/3.05.dir/parser.cpp.o: ../ch3/3.05/driver.h 28 | ch3/3.05/CMakeFiles/3.05.dir/parser.cpp.o: ../ch3/3.05/scanner.h 29 | ch3/3.05/CMakeFiles/3.05.dir/parser.cpp.o: ch3/3.05/location.hh 30 | ch3/3.05/CMakeFiles/3.05.dir/parser.cpp.o: ch3/3.05/parser.cpp 31 | ch3/3.05/CMakeFiles/3.05.dir/parser.cpp.o: ch3/3.05/parser.hpp 32 | ch3/3.05/CMakeFiles/3.05.dir/parser.cpp.o: ch3/3.05/position.hh 33 | ch3/3.05/CMakeFiles/3.05.dir/parser.cpp.o: ch3/3.05/stack.hh 34 | 35 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.05/CMakeFiles/3.05.dir/depend.internal: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | ch3/3.05/CMakeFiles/3.05.dir/driver.cpp.o 5 | /home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.05/driver.cpp 6 | /home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.05/driver.h 7 | /home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.05/scanner.h 8 | ch3/3.05/location.hh 9 | ch3/3.05/parser.hpp 10 | ch3/3.05/position.hh 11 | ch3/3.05/stack.hh 12 | ch3/3.05/CMakeFiles/3.05.dir/lexer.cpp.o 13 | ../ch3/3.05/scanner.h 14 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/lexer.cpp 15 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/location.hh 16 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/parser.hpp 17 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/position.hh 18 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/stack.hh 19 | ch3/3.05/CMakeFiles/3.05.dir/main.cpp.o 20 | /home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.05/driver.h 21 | /home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.05/main.cpp 22 | /home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.05/scanner.h 23 | ch3/3.05/location.hh 24 | ch3/3.05/parser.hpp 25 | ch3/3.05/position.hh 26 | ch3/3.05/stack.hh 27 | ch3/3.05/CMakeFiles/3.05.dir/parser.cpp.o 28 | ../ch3/3.05/driver.h 29 | ../ch3/3.05/scanner.h 30 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/location.hh 31 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/parser.cpp 32 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/parser.hpp 33 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/position.hh 34 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/stack.hh 35 | -------------------------------------------------------------------------------- /cmake-build-debug/cmake_install.cmake: -------------------------------------------------------------------------------- 1 | # Install script for directory: /home/cmp/work_dir/source_code/yacc_bison_practice 2 | 3 | # Set the install prefix 4 | if(NOT DEFINED CMAKE_INSTALL_PREFIX) 5 | set(CMAKE_INSTALL_PREFIX "/usr/local") 6 | endif() 7 | string(REGEX REPLACE "/$" "" CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}") 8 | 9 | # Set the install configuration name. 10 | if(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME) 11 | if(BUILD_TYPE) 12 | string(REGEX REPLACE "^[^A-Za-z0-9_]+" "" 13 | CMAKE_INSTALL_CONFIG_NAME "${BUILD_TYPE}") 14 | else() 15 | set(CMAKE_INSTALL_CONFIG_NAME "Debug") 16 | endif() 17 | message(STATUS "Install configuration: \"${CMAKE_INSTALL_CONFIG_NAME}\"") 18 | endif() 19 | 20 | # Set the component getting installed. 21 | if(NOT CMAKE_INSTALL_COMPONENT) 22 | if(COMPONENT) 23 | message(STATUS "Install component: \"${COMPONENT}\"") 24 | set(CMAKE_INSTALL_COMPONENT "${COMPONENT}") 25 | else() 26 | set(CMAKE_INSTALL_COMPONENT) 27 | endif() 28 | endif() 29 | 30 | # Install shared libraries without execute permission? 31 | if(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE) 32 | set(CMAKE_INSTALL_SO_NO_EXE "1") 33 | endif() 34 | 35 | # Is this installation the result of a crosscompile? 36 | if(NOT DEFINED CMAKE_CROSSCOMPILING) 37 | set(CMAKE_CROSSCOMPILING "FALSE") 38 | endif() 39 | 40 | if(NOT CMAKE_INSTALL_LOCAL_ONLY) 41 | # Include the install script for the subdirectory. 42 | include("/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/cmake_install.cmake") 43 | endif() 44 | 45 | if(NOT CMAKE_INSTALL_LOCAL_ONLY) 46 | # Include the install script for the subdirectory. 47 | include("/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/cmake_install.cmake") 48 | endif() 49 | 50 | if(NOT CMAKE_INSTALL_LOCAL_ONLY) 51 | # Include the install script for the subdirectory. 52 | include("/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/cmake_install.cmake") 53 | endif() 54 | 55 | if(CMAKE_INSTALL_COMPONENT) 56 | set(CMAKE_INSTALL_MANIFEST "install_manifest_${CMAKE_INSTALL_COMPONENT}.txt") 57 | else() 58 | set(CMAKE_INSTALL_MANIFEST "install_manifest.txt") 59 | endif() 60 | 61 | string(REPLACE ";" "\n" CMAKE_INSTALL_MANIFEST_CONTENT 62 | "${CMAKE_INSTALL_MANIFEST_FILES}") 63 | file(WRITE "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/${CMAKE_INSTALL_MANIFEST}" 64 | "${CMAKE_INSTALL_MANIFEST_CONTENT}") 65 | -------------------------------------------------------------------------------- /ch3/3.05/lexer.l: -------------------------------------------------------------------------------- 1 | /* file: lexer.h*/ 2 | 3 | %{ 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include "parser.hpp" //包含由parser.y生成的头文件 11 | #include "scanner.h" //包含yyFlexLexer子类的头文件 12 | #include "location.hh" //包含位置调试信息头文件 13 | 14 | static Marker::location loc;//声明位置实例 15 | #define YY_USER_ACTION loc.columns (yyleng); /* 定义了YY_USER_ACTION,该宏在每个记号的语义动作之前被调用,来根据记号的长度设置位置的信息 */ 16 | 17 | #undef yywrap 18 | #define yywrap() 1 19 | 20 | using namespace Marker; 21 | #define yyterminate() Parser::make_END(loc); 22 | %} 23 | /* 声明使用C++版本FLEXER,c++是大小写不敏感的 */ 24 | %option c++ 25 | 26 | /* 支持调试 */ 27 | %option noyywrap debug 28 | 29 | /* 使用Scanner::yylex() */ 30 | %option yyclass="Scanner" 31 | 32 | /* 一些与编译常量使用该前缀否则为yy */ 33 | %option prefix="Marker_" 34 | 35 | /*正则式的各种简写,使程序有层次感而显得明晰 */ 36 | string \"[^\n"]+\" 37 | /**/ 38 | ws [ \t]+ 39 | alpha [A-Za-z] 40 | dig [0-9] 41 | name ({alpha}|{dig}|\$)({alpha}|{dig}|[_.\-/$])* 42 | num1 {dig}+\.?([eE][-]?{dig}+)? 43 | num2 {dig}*\.{dig}+([eE][-]?{dig}+)? 44 | number {num1}|{num2} 45 | id ({alpha})+ 46 | 47 | 48 | %% 49 | %{ 50 | // C++ 兼容的词法分析器的规则,step函数把位置的起始值设置为与结束值相等,这样位置就指向了上一个极少的结束位置。 51 | loc.step(); 52 | %} 53 | 54 | 55 | {number} { 56 | return Parser::make_NUMBER(std::strtof(yytext,(&yytext+yyleng)),loc); // strtof函数将字符串转换为浮点数 57 | } 58 | 59 | "//".* | 60 | [ \t] { 61 | /* 跳过注释和空白符号 */ 62 | // step函数把位置的起始值设置为与结束值相等,这样位置就指向了上一个极少的结束位置。 63 | // 由于注释和空白符号识别后并不会返回,而前一个step的调用是在上一次yylex返回时,所以此处需要手动更新记号的起始位置 64 | loc.step(); 65 | } 66 | 67 | \n { 68 | loc.lines(yyleng); // 使用lines函数来更新位置信息中的行号 69 | loc.step(); 70 | return Parser::make_EOL(loc); 71 | } 72 | "+" { return Parser::make_ADD(loc); } 73 | "-" { return Parser::make_SUB(loc); } 74 | "*" { return Parser::make_MUL(loc); } 75 | "/" { return Parser::make_DIV(loc); } 76 | "(" { return Parser::make_LPAREN(yytext,loc); } 77 | ")" { return Parser::make_RPAREN(yytext,loc); } 78 | "|" { return Parser::make_ABS(loc); } 79 | {id} { return Parser::make_IDENTIFIER(yytext,loc); } 80 | <> { return yyterminate(); } 81 | . { 82 | printf("Mystery character %c\n", *yytext); 83 | loc.step(); 84 | } 85 | %% 86 | //这里可以放一些C或者C++代码 -------------------------------------------------------------------------------- /cmake-build-debug/ch1/1.06/1.06_calculator_with_bison.tab.h: -------------------------------------------------------------------------------- 1 | /* A Bison parser, made by GNU Bison 3.0.4. */ 2 | 3 | /* Bison interface for Yacc-like parsers in C 4 | 5 | Copyright (C) 1984, 1989-1990, 2000-2015 Free Software Foundation, Inc. 6 | 7 | This program is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this program. If not, see . */ 19 | 20 | /* As a special exception, you may create a larger work that contains 21 | part or all of the Bison parser skeleton and distribute that work 22 | under terms of your choice, so long as that work isn't itself a 23 | parser generator using the skeleton or a modified version thereof 24 | as a parser skeleton. Alternatively, if you modify or redistribute 25 | the parser skeleton itself, you may (at your option) remove this 26 | special exception, which will cause the skeleton and the resulting 27 | Bison output files to be licensed under the GNU General Public 28 | License without this special exception. 29 | 30 | This special exception was added by the Free Software Foundation in 31 | version 2.2 of Bison. */ 32 | 33 | #ifndef YY_YY_1_06_CALCULATOR_WITH_BISON_TAB_H_INCLUDED 34 | # define YY_YY_1_06_CALCULATOR_WITH_BISON_TAB_H_INCLUDED 35 | /* Debug traces. */ 36 | #ifndef YYDEBUG 37 | # define YYDEBUG 0 38 | #endif 39 | #if YYDEBUG 40 | extern int yydebug; 41 | #endif 42 | 43 | /* Token type. */ 44 | #ifndef YYTOKENTYPE 45 | # define YYTOKENTYPE 46 | enum yytokentype 47 | { 48 | NUMBER = 258, 49 | ADD = 259, 50 | SUB = 260, 51 | MUL = 261, 52 | DIV = 262, 53 | ABS = 263, 54 | L_PARENTHESIS = 264, 55 | R_PARENTHESIS = 265, 56 | EOL = 266 57 | }; 58 | #endif 59 | 60 | /* Value type. */ 61 | #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED 62 | typedef int YYSTYPE; 63 | # define YYSTYPE_IS_TRIVIAL 1 64 | # define YYSTYPE_IS_DECLARED 1 65 | #endif 66 | 67 | 68 | extern YYSTYPE yylval; 69 | 70 | int yyparse (void); 71 | 72 | #endif /* !YY_YY_1_06_CALCULATOR_WITH_BISON_TAB_H_INCLUDED */ 73 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.02/3.02_create_AST_with_bison.tab.h: -------------------------------------------------------------------------------- 1 | /* A Bison parser, made by GNU Bison 3.0.4. */ 2 | 3 | /* Bison interface for Yacc-like parsers in C 4 | 5 | Copyright (C) 1984, 1989-1990, 2000-2015 Free Software Foundation, Inc. 6 | 7 | This program is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this program. If not, see . */ 19 | 20 | /* As a special exception, you may create a larger work that contains 21 | part or all of the Bison parser skeleton and distribute that work 22 | under terms of your choice, so long as that work isn't itself a 23 | parser generator using the skeleton or a modified version thereof 24 | as a parser skeleton. Alternatively, if you modify or redistribute 25 | the parser skeleton itself, you may (at your option) remove this 26 | special exception, which will cause the skeleton and the resulting 27 | Bison output files to be licensed under the GNU General Public 28 | License without this special exception. 29 | 30 | This special exception was added by the Free Software Foundation in 31 | version 2.2 of Bison. */ 32 | 33 | #ifndef YY_YY_3_02_CREATE_AST_WITH_BISON_TAB_H_INCLUDED 34 | # define YY_YY_3_02_CREATE_AST_WITH_BISON_TAB_H_INCLUDED 35 | /* Debug traces. */ 36 | #ifndef YYDEBUG 37 | # define YYDEBUG 0 38 | #endif 39 | #if YYDEBUG 40 | extern int yydebug; 41 | #endif 42 | 43 | /* Token type. */ 44 | #ifndef YYTOKENTYPE 45 | # define YYTOKENTYPE 46 | enum yytokentype 47 | { 48 | NUMBER = 258, 49 | EOL = 259 50 | }; 51 | #endif 52 | 53 | /* Value type. */ 54 | #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED 55 | 56 | union YYSTYPE 57 | { 58 | #line 10 "/home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.02/3.02_create_AST_with_bison.y" /* yacc.c:1909 */ 59 | 60 | struct ASTNode *a; 61 | double d; 62 | 63 | #line 64 "3.02_create_AST_with_bison.tab.h" /* yacc.c:1909 */ 64 | }; 65 | 66 | typedef union YYSTYPE YYSTYPE; 67 | # define YYSTYPE_IS_TRIVIAL 1 68 | # define YYSTYPE_IS_DECLARED 1 69 | #endif 70 | 71 | 72 | extern YYSTYPE yylval; 73 | 74 | int yyparse (void); 75 | 76 | #endif /* !YY_YY_3_02_CREATE_AST_WITH_BISON_TAB_H_INCLUDED */ 77 | -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/TargetDirectories.txt: -------------------------------------------------------------------------------- 1 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/CMakeFiles/rebuild_cache.dir 2 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/CMakeFiles/edit_cache.dir 3 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/CMakeFiles/rebuild_cache.dir 4 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/CMakeFiles/edit_cache.dir 5 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/CMakeFiles/1.01_word_counter.dir 6 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/CMakeFiles/1.03_recognize_calculator_tokens.dir 7 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir 8 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/CMakeFiles/1.04_NextToken_with_Flex.dir 9 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/1.06/CMakeFiles/rebuild_cache.dir 10 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/1.06/CMakeFiles/edit_cache.dir 11 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/1.06/CMakeFiles/1.06.dir 12 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/CMakeFiles/rebuild_cache.dir 13 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/CMakeFiles/edit_cache.dir 14 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/CMakeFiles/2.01_usual_lang_lex_re.dir 15 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/CMakeFiles/2.02_use_yyin_to_read_data_from_file.dir 16 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/CMakeFiles/2.03_read_files.dir 17 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/CMakeFiles/2.06_include_file_and_start_state.dir 18 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/CMakeFiles/rebuild_cache.dir 19 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/CMakeFiles/edit_cache.dir 20 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.02/CMakeFiles/edit_cache.dir 21 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.02/CMakeFiles/rebuild_cache.dir 22 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.02/CMakeFiles/3.02_create_AST_with_bison.dir 23 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/CMakeFiles/rebuild_cache.dir 24 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/CMakeFiles/edit_cache.dir 25 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/CMakeFiles/3.05.dir 26 | -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/3.16.5/CMakeCCompiler.cmake: -------------------------------------------------------------------------------- 1 | set(CMAKE_C_COMPILER "/usr/bin/cc") 2 | set(CMAKE_C_COMPILER_ARG1 "") 3 | set(CMAKE_C_COMPILER_ID "GNU") 4 | set(CMAKE_C_COMPILER_VERSION "7.4.0") 5 | set(CMAKE_C_COMPILER_VERSION_INTERNAL "") 6 | set(CMAKE_C_COMPILER_WRAPPER "") 7 | set(CMAKE_C_STANDARD_COMPUTED_DEFAULT "11") 8 | set(CMAKE_C_COMPILE_FEATURES "c_std_90;c_function_prototypes;c_std_99;c_restrict;c_variadic_macros;c_std_11;c_static_assert") 9 | set(CMAKE_C90_COMPILE_FEATURES "c_std_90;c_function_prototypes") 10 | set(CMAKE_C99_COMPILE_FEATURES "c_std_99;c_restrict;c_variadic_macros") 11 | set(CMAKE_C11_COMPILE_FEATURES "c_std_11;c_static_assert") 12 | 13 | set(CMAKE_C_PLATFORM_ID "Linux") 14 | set(CMAKE_C_SIMULATE_ID "") 15 | set(CMAKE_C_COMPILER_FRONTEND_VARIANT "") 16 | set(CMAKE_C_SIMULATE_VERSION "") 17 | 18 | 19 | 20 | set(CMAKE_AR "/usr/bin/ar") 21 | set(CMAKE_C_COMPILER_AR "/usr/bin/gcc-ar-7") 22 | set(CMAKE_RANLIB "/usr/bin/ranlib") 23 | set(CMAKE_C_COMPILER_RANLIB "/usr/bin/gcc-ranlib-7") 24 | set(CMAKE_LINKER "/usr/bin/ld") 25 | set(CMAKE_MT "") 26 | set(CMAKE_COMPILER_IS_GNUCC 1) 27 | set(CMAKE_C_COMPILER_LOADED 1) 28 | set(CMAKE_C_COMPILER_WORKS TRUE) 29 | set(CMAKE_C_ABI_COMPILED TRUE) 30 | set(CMAKE_COMPILER_IS_MINGW ) 31 | set(CMAKE_COMPILER_IS_CYGWIN ) 32 | if(CMAKE_COMPILER_IS_CYGWIN) 33 | set(CYGWIN 1) 34 | set(UNIX 1) 35 | endif() 36 | 37 | set(CMAKE_C_COMPILER_ENV_VAR "CC") 38 | 39 | if(CMAKE_COMPILER_IS_MINGW) 40 | set(MINGW 1) 41 | endif() 42 | set(CMAKE_C_COMPILER_ID_RUN 1) 43 | set(CMAKE_C_SOURCE_FILE_EXTENSIONS c;m) 44 | set(CMAKE_C_IGNORE_EXTENSIONS h;H;o;O;obj;OBJ;def;DEF;rc;RC) 45 | set(CMAKE_C_LINKER_PREFERENCE 10) 46 | 47 | # Save compiler ABI information. 48 | set(CMAKE_C_SIZEOF_DATA_PTR "8") 49 | set(CMAKE_C_COMPILER_ABI "ELF") 50 | set(CMAKE_C_LIBRARY_ARCHITECTURE "x86_64-linux-gnu") 51 | 52 | if(CMAKE_C_SIZEOF_DATA_PTR) 53 | set(CMAKE_SIZEOF_VOID_P "${CMAKE_C_SIZEOF_DATA_PTR}") 54 | endif() 55 | 56 | if(CMAKE_C_COMPILER_ABI) 57 | set(CMAKE_INTERNAL_PLATFORM_ABI "${CMAKE_C_COMPILER_ABI}") 58 | endif() 59 | 60 | if(CMAKE_C_LIBRARY_ARCHITECTURE) 61 | set(CMAKE_LIBRARY_ARCHITECTURE "x86_64-linux-gnu") 62 | endif() 63 | 64 | set(CMAKE_C_CL_SHOWINCLUDES_PREFIX "") 65 | if(CMAKE_C_CL_SHOWINCLUDES_PREFIX) 66 | set(CMAKE_CL_SHOWINCLUDES_PREFIX "${CMAKE_C_CL_SHOWINCLUDES_PREFIX}") 67 | endif() 68 | 69 | 70 | 71 | 72 | 73 | set(CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES "/usr/lib/gcc/x86_64-linux-gnu/7/include;/usr/local/include;/usr/lib/gcc/x86_64-linux-gnu/7/include-fixed;/usr/include/x86_64-linux-gnu;/usr/include") 74 | set(CMAKE_C_IMPLICIT_LINK_LIBRARIES "gcc;gcc_s;c;gcc;gcc_s") 75 | set(CMAKE_C_IMPLICIT_LINK_DIRECTORIES "/usr/lib/gcc/x86_64-linux-gnu/7;/usr/lib/x86_64-linux-gnu;/usr/lib;/lib/x86_64-linux-gnu;/lib") 76 | set(CMAKE_C_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "") 77 | -------------------------------------------------------------------------------- /ch3/3.02/fb3.02.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "fb3.02.h" 5 | 6 | int yyparse(void); 7 | 8 | /* 为操作符,创建一个子树 */ 9 | struct ASTNode *newast(int NodeType, struct ASTNode *Lft, struct ASTNode *Rht) { 10 | struct ASTNode *a = malloc(sizeof(struct ASTNode)); 11 | if (!a) { 12 | yyerror("out of memory!"); 13 | exit(0); 14 | } 15 | a->NodeType = NodeType; 16 | a->Lft = Lft; 17 | a->Rht = Rht; 18 | return a; 19 | } 20 | 21 | /* 为一个值,创建一个叶子节点 */ 22 | struct ASTNode *newnum(double d) { 23 | struct numval *a = malloc(sizeof(struct numval)); 24 | if (!a) { 25 | yyerror("out of memory!"); 26 | exit(0); 27 | } 28 | a->NodeType = NT_NUM; 29 | a->number = d; 30 | 31 | // 此处将"struct numval"的类型强制转换为"struct ASTNode"类型,是因为能够统一适配eval函数的入参类型; 32 | // 而eval函数能够正确运行得益于"struct numval"类型和"struct ASTNode"类型中nodetype字段在内存中的相对位置是一致的。 33 | // 通过nodetype字段识别到"struct numval"类型的实例时,要使用该实例需要强制转换回来才行。 34 | return (struct ASTNode *) a; 35 | } 36 | 37 | /* 递归计算各个子树的值 */ 38 | double eval(struct ASTNode *a) { 39 | double v = 0; // result of eval 40 | 41 | switch (a->NodeType) { 42 | case NT_NUM: 43 | v = ((struct numval *) a)->number; 44 | break; 45 | case NT_ADD: 46 | v = eval(a->Lft) + eval(a->Rht); 47 | break; 48 | case NT_SUB: 49 | v = eval(a->Lft) - eval(a->Rht); 50 | break; 51 | case NT_MUL: 52 | v = eval(a->Lft) * eval(a->Rht); 53 | break; 54 | case NT_DIV: 55 | v = eval(a->Lft) / eval(a->Rht); 56 | break; 57 | case NT_ABS: 58 | v = eval(a->Lft); 59 | v = v < 0 ? v * -1.0f : v; 60 | break; 61 | case NT_NEG: // 处理负数求值 62 | v = eval(a->Lft) * -1.0f; 63 | break; 64 | 65 | default: 66 | printf("internal error:bad NodeType=%c,%d\n", 67 | a->NodeType, a->NodeType); 68 | break; 69 | } 70 | return v; 71 | } 72 | 73 | /* 递归释放各个树节点 */ 74 | void treefree(struct ASTNode *a) { 75 | switch (a->NodeType) { 76 | case NT_ADD:/* 2颗子树 */ 77 | case NT_SUB: 78 | case NT_MUL: 79 | case NT_DIV: 80 | treefree(a->Lft); 81 | treefree(a->Rht); 82 | break; 83 | case NT_ABS: /* 1颗子树 */ 84 | case NT_NEG: 85 | treefree(a->Lft); 86 | break; 87 | case NT_NUM:/* 没有子树 */ 88 | free(a); 89 | a = NULL; 90 | break; 91 | default: 92 | printf("internal error:bad NodeType=%c,%d\n", 93 | a->NodeType, a->NodeType); 94 | break; 95 | } 96 | } 97 | 98 | void yyerror(char *s, ...) { 99 | va_list ap; 100 | va_start(ap, s); 101 | fprintf(stderr, "%d: error: ", yylineno); 102 | vfprintf(stderr, s, ap); 103 | fprintf(stderr, "\n"); 104 | } 105 | 106 | int main(int argc, char **argv) { 107 | printf("> "); 108 | return yyparse(); 109 | } -------------------------------------------------------------------------------- /ch3/3.05/parser.y: -------------------------------------------------------------------------------- 1 | /* file: parser.y */ 2 | 3 | /* 使用指令%skeleton "lalr1.cc"选择C++解析器的骨架 */ 4 | %skeleton "lalr1.cc" 5 | 6 | /* 指定bison的版本 */ 7 | %require "3.0.4" 8 | 9 | %define api.namespace {Marker} //声明命名空间与下面声明的类名结合使用 Marker::Parser:: 在scanner.l中有体现 10 | %define parser_class_name { Parser } 11 | %define api.token.constructor 12 | %define api.value.type variant //使得类型与token定义可以使用各种复杂的结构与类型 13 | %define parse.assert //开启断言功能 14 | %defines //生成各种头文件 location.hh position.hh parser.hpp 15 | %code requires 16 | { 17 | /*requires中的内容会放在YYLTYPE与YYSTPYPE定义前*/ 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | using namespace std; 24 | 25 | namespace Marker { /*避免包含头文件时冲突*/ 26 | class Scanner; 27 | class Driver; 28 | } 29 | } 30 | 31 | %code top 32 | { 33 | /*尽可能放在parser.cpp靠近头部的地方,与requires相似*/ 34 | #include 35 | #include "scanner.h" 36 | #include "parser.hpp" 37 | #include "driver.h" 38 | #include "location.hh" 39 | 40 | /*注意:这里的参数由%parse-param决定*/ 41 | static Marker::Parser::symbol_type yylex(Marker::Scanner& scanner,Marker::Driver &driver){ 42 | return scanner.nextToken(); 43 | } 44 | using namespace Marker; 45 | } 46 | 47 | /*定义parser传给scanner的参数*/ 48 | %lex-param { Marker::Scanner& scanner } 49 | %lex-param { Marker::Driver& driver } 50 | 51 | /*定义driver传给parser的参数*/ 52 | %parse-param { Marker::Scanner& scanner } 53 | %parse-param { Marker::Driver& driver } 54 | 55 | %locations 56 | //%define parse-trace 57 | 58 | /*详细显示错误信息*/ 59 | %define parse.error verbose 60 | 61 | /*通过Marker::Parser::make_XXX(loc)给token添加前缀*/ 62 | %define api.token.prefix {TOKEN_} 63 | 64 | %token RPAREN 65 | %token IDENTIFIER 66 | %token NUMBER 67 | 68 | %token EOL 69 | %token END 0 70 | 71 | %left ADD "+" 72 | %left SUB "-" 73 | %left MUL "*" 74 | %left DIV "/" 75 | 76 | %nonassoc ABS "|" 77 | 78 | %nonassoc NEG // 负号具有最高优先级但没有结合性 79 | 80 | %left LPAREN 81 | 82 | %type exp calclist factor term 83 | 84 | %start calclist 85 | 86 | %% 87 | calclist: %empty { /* 使用%empty显示的声明该规则是一个空规则 */ } 88 | | calclist exp EOL { cout << "=" << $2 << "\n>"; } 89 | ; 90 | 91 | exp: factor { $$ = $1; } 92 | | exp ADD factor { $$ = $1 + $3; } 93 | | exp SUB factor { $$ = $1 - $3; } 94 | ; 95 | 96 | factor: term { $$ = $1; } 97 | | factor MUL term { $$ = $1 * $3; } 98 | | factor DIV term { 99 | if($3 == 0){ 100 | error(@3, "zero divide"); 101 | YYABORT; 102 | } 103 | $$ = $1 / $3; 104 | } 105 | ; 106 | 107 | term: NUMBER { $$ = $1; } 108 | | ABS exp ABS { $$ = $2 >= 0 ? $2 : -1 * $2; } 109 | | LPAREN exp RPAREN { $$ = $2; } 110 | | SUB exp %prec NEG { $$ = -1 * $2; } 111 | ; 112 | 113 | %% 114 | /*Parser实现错误处理接口*/ 115 | void Marker::Parser::error(const Marker::location& location,const std::string& message){ 116 | std::cout<<"msg:"< 6 | #include 7 | #include "1.04_NextToken_with_Flex.h" 8 | 9 | void printToken(int token) { 10 | switch (token) { 11 | case INTEGER : { 12 | printf("INTEGER %d\n", atoi(yytext)); 13 | break; 14 | } 15 | case DECIMAL : { 16 | printf("DECIMAL %f\n", atof(yytext)); 17 | break; 18 | } 19 | case IDENTIFIER : { 20 | printf("IDENTIFIER %s\n", yytext); 21 | break; 22 | } 23 | case OPERATOR : { 24 | printf("OPERATOR %s\n", yytext); 25 | break; 26 | } 27 | case KEYWORD : { 28 | printf("KEYWORD %s\n", yytext); 29 | break; 30 | } 31 | case L_PARENTHESIS : { 32 | printf("L_PARENTHESIS %s\n", yytext); 33 | break; 34 | } 35 | case R_PARENTHESIS : { 36 | printf("R_PARENTHESIS %s\n", yytext); 37 | break; 38 | } 39 | case L_SQUARE_BRACKET : { 40 | printf("L_SQUARE_BRACKET %s\n", yytext); 41 | break; 42 | } 43 | case R_SQUARE_BRACKET : { 44 | printf("R_SQUARE_BRACKET %s\n", yytext); 45 | break; 46 | } 47 | case L_BRACE : { 48 | printf("L_BRACE %s\n", yytext); 49 | break; 50 | } 51 | case R_BRACE : { 52 | printf("R_BRACE %s\n", yytext); 53 | break; 54 | } 55 | case QUESTION_SIGN : { 56 | printf("QUESTION_SIGN %s\n", yytext); 57 | break; 58 | } 59 | case COMMA : { 60 | printf("COMMA %s\n", yytext); 61 | break; 62 | } 63 | case COLON : { 64 | printf("COLON %s\n", yytext); 65 | break; 66 | } 67 | case SEMICOLON : { 68 | printf("SEMICOLON %s\n", yytext); 69 | break; 70 | } 71 | case POUND_SIGN : { 72 | printf("POUND_SIGN %s\n", yytext); 73 | break; 74 | } 75 | case LESS_THAN : { 76 | printf("LESS_THAN %s\n", yytext); 77 | break; 78 | } 79 | case LESS_EQUAL_THAN : { 80 | printf("LESS_EQUAL_THAN %s\n", yytext); 81 | break; 82 | } 83 | case GREAT_THAN : { 84 | printf("GREAT_THAN %s\n", yytext); 85 | break; 86 | } 87 | case GREAT_EQUAL_THAN : { 88 | printf("GREAT_EQUAL_THAN %s\n", yytext); 89 | break; 90 | } 91 | case QUOTATION : { 92 | printf("QUOTATION %s\n", yytext); 93 | break; 94 | } 95 | case STRING : { 96 | printf("STRING %s\n", yytext); 97 | break; 98 | } 99 | case ANNOTATION : { 100 | printf("ANNOTATION %s\n", yytext); 101 | break; 102 | } 103 | case MULTI_LINE_ANNOTATION : { 104 | printf("MULTI_LINE_ANNOTATION %s\n", yytext); 105 | break; 106 | } 107 | 108 | default: 109 | printf("[E]>>>Unknown TokenType %d\n", token); 110 | } 111 | } 112 | 113 | int main() { 114 | int token = 0; 115 | while (token = NextToken()) { 116 | printToken(token); 117 | } 118 | return 0; 119 | } -------------------------------------------------------------------------------- /ch2/2.01_usual_lang_lex_re.l: -------------------------------------------------------------------------------- 1 | /* 2 | // 【汇总】Flex实现词法分析器时常见的用于定义匹配模式的RE 3 | */ 4 | 5 | %{ 6 | #include 7 | enum TokenType 8 | { 9 | UNDEFINE = 1000, 10 | ANNOTATION = 1001, // 注释 11 | INTEGER = 1002, // 整数,123 12 | DECIMAL = 1003, // 小数,123.554 13 | IDENTIFIER = 1004, // 变量,adfa 14 | STRING = 1005, // 字符串 15 | ARITHMETIC_OPT = 1006, // 四则运算符 16 | BITWISE_OPT = 1007, // 位运算符 17 | MOD = 1008, // 取模运符 % 18 | POW = 1009, // 乘方运符 ** 19 | 20 | BOOL_OPT = 1010, // 布尔运符 && || ! 21 | 22 | MULTI_LINE_ANNOTATION = 1011, // 多行注释 /**/ 23 | }; 24 | %} 25 | 26 | %% 27 | [*/+-] { printf("ARITHMETIC_OPT\n"); return ARITHMETIC_OPT;} // 四则运算符 +-*/ 28 | "%" { printf("MOD\n"); return MOD;} // 取模运符 % 29 | "**" { printf("POW\n"); return POW;} // 乘方运符 ** 30 | 31 | 32 | [~|&^] { printf("BITWISE_OPT\n"); return BITWISE_OPT;} // 位运算符 ~|&^ 33 | ">>" { printf("BITWISE_OPT\n"); return BITWISE_OPT;} // 位运算符 >> 34 | "<<" { printf("BITWISE_OPT\n"); return BITWISE_OPT;} // 位运算符 << 35 | 36 | 37 | "&&" { printf("BOOL_OPT\n"); return BOOL_OPT;} // 布尔运符 && 38 | "||" { printf("BOOL_OPT\n"); return BOOL_OPT;} // 布尔运符 || 39 | "!" { printf("BOOL_OPT\n"); return BOOL_OPT;} // 布尔运符 ! 40 | 41 | 42 | [0-9]+ { printf("INTEGER\n"); return INTEGER; } // 识别整数 43 | [a-zA-Z_][a-zA-Z0-9_]* { printf("IDENTIFIER\n"); return IDENTIFIER; } // 识别标识符 44 | (([0-9]*\.?[0-9]+)|([0-9]+\.[0-9]*))(E[+-]?[0-9]+)? { printf("DECIMAL\n"); return DECIMAL; } // 识别小数,支持小数的科学计数法识别 45 | \"[^"]*\" { printf("STRING\n"); return STRING; } // 识别字符串 46 | 47 | 48 | "//"[^\n]*\n { printf("ANNOTATION\n"); return ANNOTATION; } // 识别单行注释 49 | "/*"([^*]|\*+[^/*])*"*/" { printf("MULTI_LINE_ANNOTATION\n"); return MULTI_LINE_ANNOTATION; } // 识别多行注释 50 | 51 | 52 | \n { printf("NEWLINE\n"); return UNDEFINE; } // 识别换行符号 53 | [ \t] { } // 忽略空格 54 | . { printf("Mystery character %s\n", yytext); } // 忽略未定义字符串 55 | %% 56 | 57 | /* 58 | // 细心的朋友已经发现,对于整数字符串(如:"123"),能够同时匹配以上的第1个规则和第3个规则。 59 | // 下面一起来认识以下Flex是如何处理"相同的输入可能被多种不同的模式匹配"这种情况的: 60 | // 1. Flex将匹配尽可能多的的字符串 61 | // 2. 如果根据1处理后,仍然存在多个模式的话,Flex将选择更早定义的模式来匹配 62 | // 63 | */ 64 | 65 | 66 | //注意: 67 | // 对于识别多行注释: 68 | // "/*"([^*]|\*+[^/*])*"*/" { return MULTI_LINE_ANNOTATION; } // 识别多行注释 69 | // 是不对的,虽然能够满足大多的场景。 但有可能注释会很长,而flex的记号有一定的输入缓冲的长度限制,通常是16K。 70 | // 为了能够满足对注释的正确识别,应该使用起始条件(start condition)方法。 71 | -------------------------------------------------------------------------------- /ch2/2.06_include_file_and_start_state.l: -------------------------------------------------------------------------------- 1 | /* 2 | // file: 2.06_include_file_and_start_state.y 3 | // 使用Flex处理嵌套的包含文件(include files)与起始条件 4 | // 这个程序将处理嵌套包含文件,同时打印出这些文件中每一行的行号。 5 | // 这个程序需要维护一个包含嵌套输入文件和行号的堆栈,在每次遇到一个`#include `时压栈当前文件和行号等信息,在处理完包含文件后再把它们从堆栈弹出。 6 | // 此外本程序将使用flex一个很强大的特性——起始条件(start condition),它允许我们指定一个特定时刻哪些模式可以被用来匹配。 7 | // */ 8 | 9 | 10 | /* 不使用-lfl定义的默认main函数,使用自定义的main函数,你就不需要链接-lfl了。 */ 11 | %option noyywrap 12 | 13 | /* 把`IFILE`定义为起始条件,它将在我们寻找`#include`语句中的文件名时被使用。 */ 14 | %x IFILE 15 | 16 | %{ 17 | struct buffstack { 18 | struct buffstack *prev; /* 上一个文件信息 */ 19 | YY_BUFFER_STATE bufferstate; /* 保存的缓冲区 */ 20 | int lineno; /* 保存的行号 */ 21 | char *filename; /* 文件名 */ 22 | FILE *f; /* 当前文件 */ 23 | } *curbstk = 0; 24 | 25 | char *curfilename; /* 当前输入文件的名字 */ 26 | int newfile(char *fn); 27 | int popfile(void); 28 | %} 29 | 30 | %% 31 | ^"#"[ \t]*include[ \t]*[\"<] { // 匹配#include语句,直到双引号或者< 32 | BEGIN(IFILE); // 宏`BEGIN`用来切换到另外一个起始条件。 33 | } 34 | 35 | [^ \t\n\">]+ { // 匹配文件名,直到结束双引号、>或者换行符。 当模式紧随在<起始条件名字>之后(),表示这个模式只在该起始条件激活时才进行匹配。 36 | { // 当文件名匹配到这个模式时,#include的语句还有剩下的部分没有处理。使用下面简单的循环读完它并忽略它 37 | int c; 38 | while((c=input()) && c != '\n'); 39 | } 40 | yylineno++; 41 | if(!newfile(yytext)){ 42 | yyterminate(); // Error: no such file, or other failure. 43 | } 44 | printf("[INFO]开始读取include文件:%10s作为输入.\n", curbstk->filename); 45 | BEGIN(INITIAL); // flex本身会定义的`INITIAL`起始条件 46 | } 47 | 48 | .|\n { // 处理IFILE起始条件中错误输入的情况 49 | fprintf(stderr, "行号:%8d 错误的include语法\n", yylineno); 50 | yyterminate(); 51 | } 52 | 53 | <> { // <>是Flex定义的特殊模式,它匹配输入文件的结束。 54 | printf("[INFO]结束读取include文件:%10s.\n", curbstk->filename); 55 | if(!popfile()){ // 当文件结束时弹出文件堆栈,如果是最外层文件就结束 56 | yyterminate(); 57 | } 58 | } 59 | 60 | ^. { // 在每一行开始时,打印行号 61 | fprintf(yyout, "%8d %s", yylineno, yytext); // yylineno是Flex提供的记录行号的变量 62 | } 63 | 64 | ^\n { // 每遇到一个\n,需要把行号+1 65 | fprintf(yyout, "%8d %s", yylineno++, yytext); 66 | } 67 | 68 | \n { 69 | ECHO; 70 | yylineno++; 71 | } 72 | 73 | . { // 这是Flex定义的默认的规则,其中ECHO是Flex定义的默认输出宏,它会将字符原样输出到yyout。关于ECHO详见2.05,https://blog.csdn.net/weixin_46222091/article/details/105968391 74 | ECHO; 75 | } 76 | %% 77 | int main(int argc, char ** argv){ 78 | if(argc < 2){ 79 | fprintf(stderr, "need filename\n"); 80 | return 1; 81 | } 82 | if(newfile(argv[1])) yylex(); 83 | } 84 | 85 | int newfile(char *fn){ 86 | FILE *f = fopen(fn, "r"); 87 | struct buffstack *bstk = malloc(sizeof(struct buffstack)); 88 | 89 | /* 如果文件打开失败时,退出 */ 90 | if(!f) { 91 | perror(fn); 92 | return 0; 93 | } 94 | /* 如果没有足够空间时,退出 */ 95 | if(!bstk){ 96 | perror("malloc"); 97 | exit(1); 98 | } 99 | 100 | /* 记住当前状态 */ 101 | if(curbstk){ 102 | curbstk->lineno = yylineno; 103 | bstk->prev = curbstk; 104 | } 105 | 106 | /* 建立当前文件信息 */ 107 | bstk->bufferstate = yy_create_buffer(f, YY_BUF_SIZE); 108 | bstk->f = f; 109 | bstk->filename = fn; 110 | yy_switch_to_buffer(bstk->bufferstate); 111 | curbstk = bstk; 112 | yylineno = 1; 113 | curfilename = fn; 114 | return 1; 115 | } 116 | 117 | int popfile(void){ 118 | struct buffstack *bstk = curbstk; 119 | struct buffstack *prevbstk; 120 | 121 | if(!bstk){ 122 | return 0; 123 | } 124 | 125 | /* 删除当前文件信息 */ 126 | fclose(bstk->f); 127 | yy_delete_buffer(bstk->bufferstate); 128 | 129 | /* 切换回上一个文件 */ 130 | prevbstk = bstk->prev; 131 | free(bstk); 132 | 133 | if(!prevbstk){ 134 | return 0; 135 | } 136 | 137 | yy_switch_to_buffer(prevbstk->bufferstate); 138 | curbstk=prevbstk; 139 | yylineno = curbstk->lineno; 140 | curfilename = curbstk->filename; 141 | return 1; 142 | } -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.05/CMakeFiles/3.05.dir/CXX.includecache: -------------------------------------------------------------------------------- 1 | #IncludeRegexLine: ^[ ]*[#%][ ]*(include|import)[ ]*[<"]([^">]+)([">]) 2 | 3 | #IncludeRegexScan: ^.*$ 4 | 5 | #IncludeRegexComplain: ^$ 6 | 7 | #IncludeRegexTransform: 8 | 9 | ../ch3/3.05/driver.h 10 | iostream 11 | - 12 | scanner.h 13 | ../ch3/3.05/scanner.h 14 | parser.hpp 15 | ../ch3/3.05/parser.hpp 16 | 17 | ../ch3/3.05/scanner.h 18 | FlexLexer.h 19 | - 20 | parser.hpp 21 | ../ch3/3.05/parser.hpp 22 | 23 | /home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.05/driver.cpp 24 | driver.h 25 | /home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.05/driver.h 26 | iostream 27 | - 28 | 29 | /home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.05/driver.h 30 | iostream 31 | - 32 | scanner.h 33 | /home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.05/scanner.h 34 | parser.hpp 35 | /home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.05/parser.hpp 36 | 37 | /home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.05/main.cpp 38 | iostream 39 | - 40 | driver.h 41 | /home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.05/driver.h 42 | 43 | /home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.05/scanner.h 44 | FlexLexer.h 45 | - 46 | parser.hpp 47 | /home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.05/parser.hpp 48 | 49 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/lexer.cpp 50 | inttypes.h 51 | - 52 | iostream 53 | - 54 | errno.h 55 | - 56 | cstdlib 57 | - 58 | cstdio 59 | - 60 | cstring 61 | - 62 | FlexLexer.h 63 | - 64 | iostream 65 | - 66 | cerrno 67 | - 68 | climits 69 | - 70 | cstdlib 71 | - 72 | string 73 | - 74 | parser.hpp 75 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/parser.hpp 76 | scanner.h 77 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/scanner.h 78 | location.hh 79 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/location.hh 80 | unistd.h 81 | - 82 | 83 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/location.hh 84 | position.hh 85 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/position.hh 86 | 87 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/parser.cpp 88 | iostream 89 | - 90 | scanner.h 91 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/scanner.h 92 | parser.hpp 93 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/parser.hpp 94 | driver.h 95 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/driver.h 96 | location.hh 97 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/location.hh 98 | parser.hpp 99 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/parser.hpp 100 | libintl.h 101 | - 102 | 103 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/parser.hpp 104 | iostream 105 | - 106 | string 107 | - 108 | vector 109 | - 110 | stdint.h 111 | - 112 | cmath 113 | - 114 | cassert 115 | - 116 | cstdlib 117 | - 118 | iostream 119 | - 120 | stdexcept 121 | - 122 | string 123 | - 124 | vector 125 | - 126 | stack.hh 127 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/stack.hh 128 | location.hh 129 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/location.hh 130 | typeinfo 131 | - 132 | cassert 133 | - 134 | 135 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/position.hh 136 | algorithm 137 | - 138 | iostream 139 | - 140 | string 141 | - 142 | 143 | /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/stack.hh 144 | vector 145 | - 146 | 147 | ch3/3.05/location.hh 148 | position.hh 149 | ch3/3.05/position.hh 150 | 151 | ch3/3.05/parser.hpp 152 | iostream 153 | - 154 | string 155 | - 156 | vector 157 | - 158 | stdint.h 159 | - 160 | cmath 161 | - 162 | cassert 163 | - 164 | cstdlib 165 | - 166 | iostream 167 | - 168 | stdexcept 169 | - 170 | string 171 | - 172 | vector 173 | - 174 | stack.hh 175 | ch3/3.05/stack.hh 176 | location.hh 177 | ch3/3.05/location.hh 178 | typeinfo 179 | - 180 | cassert 181 | - 182 | 183 | ch3/3.05/position.hh 184 | algorithm 185 | - 186 | iostream 187 | - 188 | string 189 | - 190 | 191 | ch3/3.05/stack.hh 192 | vector 193 | - 194 | 195 | -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/test.dir/build.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # Delete rule output on recipe failure. 5 | .DELETE_ON_ERROR: 6 | 7 | 8 | #============================================================================= 9 | # Special targets provided by cmake. 10 | 11 | # Disable implicit rules so canonical targets will work. 12 | .SUFFIXES: 13 | 14 | 15 | # Remove some rules from gmake that .SUFFIXES does not remove. 16 | SUFFIXES = 17 | 18 | .SUFFIXES: .hpux_make_needs_suffix_list 19 | 20 | 21 | # Suppress display of executed commands. 22 | $(VERBOSE).SILENT: 23 | 24 | 25 | # A target that is always out of date. 26 | cmake_force: 27 | 28 | .PHONY : cmake_force 29 | 30 | #============================================================================= 31 | # Set environment variables for the build. 32 | 33 | # The shell in which to execute make rules. 34 | SHELL = /bin/sh 35 | 36 | # The CMake executable. 37 | CMAKE_COMMAND = /home/cmp/Downloads/clion-2019.2/bin/cmake/linux/bin/cmake 38 | 39 | # The command to remove a file. 40 | RM = /home/cmp/Downloads/clion-2019.2/bin/cmake/linux/bin/cmake -E remove -f 41 | 42 | # Escaping for special characters. 43 | EQUALS = = 44 | 45 | # The top-level source directory on which CMake was run. 46 | CMAKE_SOURCE_DIR = /home/cmp/work_dir/source_code/yacc_bison_practice 47 | 48 | # The top-level build directory on which CMake was run. 49 | CMAKE_BINARY_DIR = /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug 50 | 51 | # Include any dependencies generated for this target. 52 | include CMakeFiles/test.dir/depend.make 53 | 54 | # Include the progress variables for this target. 55 | include CMakeFiles/test.dir/progress.make 56 | 57 | # Include the compile flags for this target's objects. 58 | include CMakeFiles/test.dir/flags.make 59 | 60 | CMakeFiles/test.dir/test.c.o: CMakeFiles/test.dir/flags.make 61 | CMakeFiles/test.dir/test.c.o: test.c 62 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --progress-dir=/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/CMakeFiles --progress-num=$(CMAKE_PROGRESS_1) "Building C object CMakeFiles/test.dir/test.c.o" 63 | /usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -o CMakeFiles/test.dir/test.c.o -c /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/test.c 64 | 65 | CMakeFiles/test.dir/test.c.i: cmake_force 66 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Preprocessing C source to CMakeFiles/test.dir/test.c.i" 67 | /usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -E /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/test.c > CMakeFiles/test.dir/test.c.i 68 | 69 | CMakeFiles/test.dir/test.c.s: cmake_force 70 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Compiling C source to assembly CMakeFiles/test.dir/test.c.s" 71 | /usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -S /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/test.c -o CMakeFiles/test.dir/test.c.s 72 | 73 | # Object files for target test 74 | test_OBJECTS = \ 75 | "CMakeFiles/test.dir/test.c.o" 76 | 77 | # External object files for target test 78 | test_EXTERNAL_OBJECTS = 79 | 80 | test: CMakeFiles/test.dir/test.c.o 81 | test: CMakeFiles/test.dir/build.make 82 | test: CMakeFiles/test.dir/link.txt 83 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --bold --progress-dir=/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/CMakeFiles --progress-num=$(CMAKE_PROGRESS_2) "Linking C executable test" 84 | $(CMAKE_COMMAND) -E cmake_link_script CMakeFiles/test.dir/link.txt --verbose=$(VERBOSE) 85 | 86 | # Rule to build all files generated by this target. 87 | CMakeFiles/test.dir/build: test 88 | 89 | .PHONY : CMakeFiles/test.dir/build 90 | 91 | CMakeFiles/test.dir/clean: 92 | $(CMAKE_COMMAND) -P CMakeFiles/test.dir/cmake_clean.cmake 93 | .PHONY : CMakeFiles/test.dir/clean 94 | 95 | CMakeFiles/test.dir/depend: 96 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug && $(CMAKE_COMMAND) -E cmake_depends "Unix Makefiles" /home/cmp/work_dir/source_code/yacc_bison_practice /home/cmp/work_dir/source_code/yacc_bison_practice /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/CMakeFiles/test.dir/DependInfo.cmake --color=$(COLOR) 97 | .PHONY : CMakeFiles/test.dir/depend 98 | 99 | -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/Makefile.cmake: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # The generator used is: 5 | set(CMAKE_DEPENDS_GENERATOR "Unix Makefiles") 6 | 7 | # The top level Makefile was generated from the following files: 8 | set(CMAKE_MAKEFILE_DEPENDS 9 | "CMakeCache.txt" 10 | "/home/cmp/Downloads/clion-2019.2/bin/cmake/linux/share/cmake-3.16/Modules/CMakeCInformation.cmake" 11 | "/home/cmp/Downloads/clion-2019.2/bin/cmake/linux/share/cmake-3.16/Modules/CMakeCXXInformation.cmake" 12 | "/home/cmp/Downloads/clion-2019.2/bin/cmake/linux/share/cmake-3.16/Modules/CMakeCheckCompilerFlagCommonPatterns.cmake" 13 | "/home/cmp/Downloads/clion-2019.2/bin/cmake/linux/share/cmake-3.16/Modules/CMakeCommonLanguageInclude.cmake" 14 | "/home/cmp/Downloads/clion-2019.2/bin/cmake/linux/share/cmake-3.16/Modules/CMakeExtraGeneratorDetermineCompilerMacrosAndIncludeDirs.cmake" 15 | "/home/cmp/Downloads/clion-2019.2/bin/cmake/linux/share/cmake-3.16/Modules/CMakeFindCodeBlocks.cmake" 16 | "/home/cmp/Downloads/clion-2019.2/bin/cmake/linux/share/cmake-3.16/Modules/CMakeGenericSystem.cmake" 17 | "/home/cmp/Downloads/clion-2019.2/bin/cmake/linux/share/cmake-3.16/Modules/CMakeInitializeConfigs.cmake" 18 | "/home/cmp/Downloads/clion-2019.2/bin/cmake/linux/share/cmake-3.16/Modules/CMakeLanguageInformation.cmake" 19 | "/home/cmp/Downloads/clion-2019.2/bin/cmake/linux/share/cmake-3.16/Modules/CMakeSystemSpecificInformation.cmake" 20 | "/home/cmp/Downloads/clion-2019.2/bin/cmake/linux/share/cmake-3.16/Modules/CMakeSystemSpecificInitialize.cmake" 21 | "/home/cmp/Downloads/clion-2019.2/bin/cmake/linux/share/cmake-3.16/Modules/Compiler/CMakeCommonCompilerMacros.cmake" 22 | "/home/cmp/Downloads/clion-2019.2/bin/cmake/linux/share/cmake-3.16/Modules/Compiler/GNU-C.cmake" 23 | "/home/cmp/Downloads/clion-2019.2/bin/cmake/linux/share/cmake-3.16/Modules/Compiler/GNU-CXX.cmake" 24 | "/home/cmp/Downloads/clion-2019.2/bin/cmake/linux/share/cmake-3.16/Modules/Compiler/GNU.cmake" 25 | "/home/cmp/Downloads/clion-2019.2/bin/cmake/linux/share/cmake-3.16/Modules/Internal/CMakeCheckCompilerFlag.cmake" 26 | "/home/cmp/Downloads/clion-2019.2/bin/cmake/linux/share/cmake-3.16/Modules/Platform/Linux-GNU-C.cmake" 27 | "/home/cmp/Downloads/clion-2019.2/bin/cmake/linux/share/cmake-3.16/Modules/Platform/Linux-GNU-CXX.cmake" 28 | "/home/cmp/Downloads/clion-2019.2/bin/cmake/linux/share/cmake-3.16/Modules/Platform/Linux-GNU.cmake" 29 | "/home/cmp/Downloads/clion-2019.2/bin/cmake/linux/share/cmake-3.16/Modules/Platform/Linux.cmake" 30 | "/home/cmp/Downloads/clion-2019.2/bin/cmake/linux/share/cmake-3.16/Modules/Platform/UnixPaths.cmake" 31 | "/home/cmp/Downloads/clion-2019.2/bin/cmake/linux/share/cmake-3.16/Modules/ProcessorCount.cmake" 32 | "../CMakeLists.txt" 33 | "../ch1/1.06/CMakeLists.txt" 34 | "../ch1/CMakeLists.txt" 35 | "../ch2/CMakeLists.txt" 36 | "../ch3/3.02/CMakeLists.txt" 37 | "../ch3/3.05/CMakeLists.txt" 38 | "../ch3/CMakeLists.txt" 39 | "CMakeFiles/3.16.5/CMakeCCompiler.cmake" 40 | "CMakeFiles/3.16.5/CMakeCXXCompiler.cmake" 41 | "CMakeFiles/3.16.5/CMakeSystem.cmake" 42 | "../cmake/clean_flex_bison_generate_code.cmake" 43 | ) 44 | 45 | # The corresponding makefile is: 46 | set(CMAKE_MAKEFILE_OUTPUTS 47 | "Makefile" 48 | "CMakeFiles/cmake.check_cache" 49 | ) 50 | 51 | # Byproducts of CMake generate step: 52 | set(CMAKE_MAKEFILE_PRODUCTS 53 | "CMakeFiles/CMakeDirectoryInformation.cmake" 54 | "ch1/CMakeFiles/CMakeDirectoryInformation.cmake" 55 | "ch1/1.06/CMakeFiles/CMakeDirectoryInformation.cmake" 56 | "ch2/CMakeFiles/CMakeDirectoryInformation.cmake" 57 | "ch3/CMakeFiles/CMakeDirectoryInformation.cmake" 58 | "ch3/3.02/CMakeFiles/CMakeDirectoryInformation.cmake" 59 | "ch3/3.05/CMakeFiles/CMakeDirectoryInformation.cmake" 60 | ) 61 | 62 | # Dependency information for all targets: 63 | set(CMAKE_DEPEND_INFO_FILES 64 | "ch1/CMakeFiles/1.01_word_counter.dir/DependInfo.cmake" 65 | "ch1/CMakeFiles/1.03_recognize_calculator_tokens.dir/DependInfo.cmake" 66 | "ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/DependInfo.cmake" 67 | "ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/DependInfo.cmake" 68 | "ch1/1.06/CMakeFiles/1.06.dir/DependInfo.cmake" 69 | "ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/DependInfo.cmake" 70 | "ch2/CMakeFiles/2.02_use_yyin_to_read_data_from_file.dir/DependInfo.cmake" 71 | "ch2/CMakeFiles/2.03_read_files.dir/DependInfo.cmake" 72 | "ch2/CMakeFiles/2.06_include_file_and_start_state.dir/DependInfo.cmake" 73 | "ch3/3.02/CMakeFiles/3.02_create_AST_with_bison.dir/DependInfo.cmake" 74 | "ch3/3.05/CMakeFiles/3.05.dir/DependInfo.cmake" 75 | ) 76 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/3.05/stack.hh: -------------------------------------------------------------------------------- 1 | // A Bison parser, made by GNU Bison 3.0.4. 2 | 3 | // Stack handling for Bison parsers in C++ 4 | 5 | // Copyright (C) 2002-2015 Free Software Foundation, Inc. 6 | 7 | // This program is free software: you can redistribute it and/or modify 8 | // it under the terms of the GNU General Public License as published by 9 | // the Free Software Foundation, either version 3 of the License, or 10 | // (at your option) any later version. 11 | 12 | // This program is distributed in the hope that it will be useful, 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | // GNU General Public License for more details. 16 | 17 | // You should have received a copy of the GNU General Public License 18 | // along with this program. If not, see . 19 | 20 | // As a special exception, you may create a larger work that contains 21 | // part or all of the Bison parser skeleton and distribute that work 22 | // under terms of your choice, so long as that work isn't itself a 23 | // parser generator using the skeleton or a modified version thereof 24 | // as a parser skeleton. Alternatively, if you modify or redistribute 25 | // the parser skeleton itself, you may (at your option) remove this 26 | // special exception, which will cause the skeleton and the resulting 27 | // Bison output files to be licensed under the GNU General Public 28 | // License without this special exception. 29 | 30 | // This special exception was added by the Free Software Foundation in 31 | // version 2.2 of Bison. 32 | 33 | /** 34 | ** \file /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/stack.hh 35 | ** Define the Marker::stack class. 36 | */ 37 | 38 | #ifndef YY_YY_HOME_CMP_WORK_DIR_SOURCE_CODE_YACC_BISON_PRACTICE_CMAKE_BUILD_DEBUG_CH3_3_05_STACK_HH_INCLUDED 39 | # define YY_YY_HOME_CMP_WORK_DIR_SOURCE_CODE_YACC_BISON_PRACTICE_CMAKE_BUILD_DEBUG_CH3_3_05_STACK_HH_INCLUDED 40 | 41 | # include 42 | 43 | #line 9 "/home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.05/parser.y" // stack.hh:132 44 | namespace Marker { 45 | #line 46 "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/stack.hh" // stack.hh:132 46 | template > 47 | class stack 48 | { 49 | public: 50 | // Hide our reversed order. 51 | typedef typename S::reverse_iterator iterator; 52 | typedef typename S::const_reverse_iterator const_iterator; 53 | 54 | stack () 55 | : seq_ () 56 | { 57 | seq_.reserve (200); 58 | } 59 | 60 | stack (unsigned int n) 61 | : seq_ (n) 62 | {} 63 | 64 | inline 65 | T& 66 | operator[] (unsigned int i) 67 | { 68 | return seq_[seq_.size () - 1 - i]; 69 | } 70 | 71 | inline 72 | const T& 73 | operator[] (unsigned int i) const 74 | { 75 | return seq_[seq_.size () - 1 - i]; 76 | } 77 | 78 | /// Steal the contents of \a t. 79 | /// 80 | /// Close to move-semantics. 81 | inline 82 | void 83 | push (T& t) 84 | { 85 | seq_.push_back (T()); 86 | operator[](0).move (t); 87 | } 88 | 89 | inline 90 | void 91 | pop (unsigned int n = 1) 92 | { 93 | for (; n; --n) 94 | seq_.pop_back (); 95 | } 96 | 97 | void 98 | clear () 99 | { 100 | seq_.clear (); 101 | } 102 | 103 | inline 104 | typename S::size_type 105 | size () const 106 | { 107 | return seq_.size (); 108 | } 109 | 110 | inline 111 | const_iterator 112 | begin () const 113 | { 114 | return seq_.rbegin (); 115 | } 116 | 117 | inline 118 | const_iterator 119 | end () const 120 | { 121 | return seq_.rend (); 122 | } 123 | 124 | private: 125 | stack (const stack&); 126 | stack& operator= (const stack&); 127 | /// The wrapped container. 128 | S seq_; 129 | }; 130 | 131 | /// Present a slice of the top of a stack. 132 | template > 133 | class slice 134 | { 135 | public: 136 | slice (const S& stack, unsigned int range) 137 | : stack_ (stack) 138 | , range_ (range) 139 | {} 140 | 141 | inline 142 | const T& 143 | operator [] (unsigned int i) const 144 | { 145 | return stack_[range_ - i]; 146 | } 147 | 148 | private: 149 | const S& stack_; 150 | unsigned int range_; 151 | }; 152 | 153 | #line 9 "/home/cmp/work_dir/source_code/yacc_bison_practice/ch3/3.05/parser.y" // stack.hh:132 154 | } // Marker 155 | #line 156 "/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/3.05/stack.hh" // stack.hh:132 156 | 157 | #endif // !YY_YY_HOME_CMP_WORK_DIR_SOURCE_CODE_YACC_BISON_PRACTICE_CMAKE_BUILD_DEBUG_CH3_3_05_STACK_HH_INCLUDED 158 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/CMakeFiles/3.05.dir/build.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # Delete rule output on recipe failure. 5 | .DELETE_ON_ERROR: 6 | 7 | 8 | #============================================================================= 9 | # Special targets provided by cmake. 10 | 11 | # Disable implicit rules so canonical targets will work. 12 | .SUFFIXES: 13 | 14 | 15 | # Remove some rules from gmake that .SUFFIXES does not remove. 16 | SUFFIXES = 17 | 18 | .SUFFIXES: .hpux_make_needs_suffix_list 19 | 20 | 21 | # Suppress display of executed commands. 22 | $(VERBOSE).SILENT: 23 | 24 | 25 | # A target that is always out of date. 26 | cmake_force: 27 | 28 | .PHONY : cmake_force 29 | 30 | #============================================================================= 31 | # Set environment variables for the build. 32 | 33 | # The shell in which to execute make rules. 34 | SHELL = /bin/sh 35 | 36 | # The CMake executable. 37 | CMAKE_COMMAND = /home/cmp/Downloads/clion-2019.2/bin/cmake/linux/bin/cmake 38 | 39 | # The command to remove a file. 40 | RM = /home/cmp/Downloads/clion-2019.2/bin/cmake/linux/bin/cmake -E remove -f 41 | 42 | # Escaping for special characters. 43 | EQUALS = = 44 | 45 | # The top-level source directory on which CMake was run. 46 | CMAKE_SOURCE_DIR = /home/cmp/work_dir/source_code/yacc_bison_practice 47 | 48 | # The top-level build directory on which CMake was run. 49 | CMAKE_BINARY_DIR = /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug 50 | 51 | # Include any dependencies generated for this target. 52 | include ch3/CMakeFiles/3.05.dir/depend.make 53 | 54 | # Include the progress variables for this target. 55 | include ch3/CMakeFiles/3.05.dir/progress.make 56 | 57 | # Include the compile flags for this target's objects. 58 | include ch3/CMakeFiles/3.05.dir/flags.make 59 | 60 | ch3/CMakeFiles/3.05.dir/__/3.05lexer.c.o: ch3/CMakeFiles/3.05.dir/flags.make 61 | ch3/CMakeFiles/3.05.dir/__/3.05lexer.c.o: 3.05lexer.c 62 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --progress-dir=/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/CMakeFiles --progress-num=$(CMAKE_PROGRESS_1) "Building C object ch3/CMakeFiles/3.05.dir/__/3.05lexer.c.o" 63 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3 && /usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -o CMakeFiles/3.05.dir/__/3.05lexer.c.o -c /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/3.05lexer.c 64 | 65 | ch3/CMakeFiles/3.05.dir/__/3.05lexer.c.i: cmake_force 66 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Preprocessing C source to CMakeFiles/3.05.dir/__/3.05lexer.c.i" 67 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3 && /usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -E /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/3.05lexer.c > CMakeFiles/3.05.dir/__/3.05lexer.c.i 68 | 69 | ch3/CMakeFiles/3.05.dir/__/3.05lexer.c.s: cmake_force 70 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Compiling C source to assembly CMakeFiles/3.05.dir/__/3.05lexer.c.s" 71 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3 && /usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -S /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/3.05lexer.c -o CMakeFiles/3.05.dir/__/3.05lexer.c.s 72 | 73 | # Object files for target 3.05 74 | 3_05_OBJECTS = \ 75 | "CMakeFiles/3.05.dir/__/3.05lexer.c.o" 76 | 77 | # External object files for target 3.05 78 | 3_05_EXTERNAL_OBJECTS = 79 | 80 | ch3/3.05: ch3/CMakeFiles/3.05.dir/__/3.05lexer.c.o 81 | ch3/3.05: ch3/CMakeFiles/3.05.dir/build.make 82 | ch3/3.05: ch3/CMakeFiles/3.05.dir/link.txt 83 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --bold --progress-dir=/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/CMakeFiles --progress-num=$(CMAKE_PROGRESS_2) "Linking C executable 3.05" 84 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3 && $(CMAKE_COMMAND) -E cmake_link_script CMakeFiles/3.05.dir/link.txt --verbose=$(VERBOSE) 85 | 86 | # Rule to build all files generated by this target. 87 | ch3/CMakeFiles/3.05.dir/build: ch3/3.05 88 | 89 | .PHONY : ch3/CMakeFiles/3.05.dir/build 90 | 91 | ch3/CMakeFiles/3.05.dir/clean: 92 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3 && $(CMAKE_COMMAND) -P CMakeFiles/3.05.dir/cmake_clean.cmake 93 | .PHONY : ch3/CMakeFiles/3.05.dir/clean 94 | 95 | ch3/CMakeFiles/3.05.dir/depend: 96 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug && $(CMAKE_COMMAND) -E cmake_depends "Unix Makefiles" /home/cmp/work_dir/source_code/yacc_bison_practice /home/cmp/work_dir/source_code/yacc_bison_practice/ch3 /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3 /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/CMakeFiles/3.05.dir/DependInfo.cmake --color=$(COLOR) 97 | .PHONY : ch3/CMakeFiles/3.05.dir/depend 98 | 99 | -------------------------------------------------------------------------------- /cmake-build-debug/ch3/Makefile: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # Default target executed when no arguments are given to make. 5 | default_target: all 6 | 7 | .PHONY : default_target 8 | 9 | # Allow only one "make -f Makefile2" at a time, but pass parallelism. 10 | .NOTPARALLEL: 11 | 12 | 13 | #============================================================================= 14 | # Special targets provided by cmake. 15 | 16 | # Disable implicit rules so canonical targets will work. 17 | .SUFFIXES: 18 | 19 | 20 | # Remove some rules from gmake that .SUFFIXES does not remove. 21 | SUFFIXES = 22 | 23 | .SUFFIXES: .hpux_make_needs_suffix_list 24 | 25 | 26 | # Suppress display of executed commands. 27 | $(VERBOSE).SILENT: 28 | 29 | 30 | # A target that is always out of date. 31 | cmake_force: 32 | 33 | .PHONY : cmake_force 34 | 35 | #============================================================================= 36 | # Set environment variables for the build. 37 | 38 | # The shell in which to execute make rules. 39 | SHELL = /bin/sh 40 | 41 | # The CMake executable. 42 | CMAKE_COMMAND = /home/cmp/Downloads/clion-2019.2/bin/cmake/linux/bin/cmake 43 | 44 | # The command to remove a file. 45 | RM = /home/cmp/Downloads/clion-2019.2/bin/cmake/linux/bin/cmake -E remove -f 46 | 47 | # Escaping for special characters. 48 | EQUALS = = 49 | 50 | # The top-level source directory on which CMake was run. 51 | CMAKE_SOURCE_DIR = /home/cmp/work_dir/source_code/yacc_bison_practice 52 | 53 | # The top-level build directory on which CMake was run. 54 | CMAKE_BINARY_DIR = /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug 55 | 56 | #============================================================================= 57 | # Targets provided globally by CMake. 58 | 59 | # Special rule for the target rebuild_cache 60 | rebuild_cache: 61 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..." 62 | /home/cmp/Downloads/clion-2019.2/bin/cmake/linux/bin/cmake -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) 63 | .PHONY : rebuild_cache 64 | 65 | # Special rule for the target rebuild_cache 66 | rebuild_cache/fast: rebuild_cache 67 | 68 | .PHONY : rebuild_cache/fast 69 | 70 | # Special rule for the target edit_cache 71 | edit_cache: 72 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "No interactive CMake dialog available..." 73 | /home/cmp/Downloads/clion-2019.2/bin/cmake/linux/bin/cmake -E echo No\ interactive\ CMake\ dialog\ available. 74 | .PHONY : edit_cache 75 | 76 | # Special rule for the target edit_cache 77 | edit_cache/fast: edit_cache 78 | 79 | .PHONY : edit_cache/fast 80 | 81 | # The main all target 82 | all: cmake_check_build_system 83 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug && $(CMAKE_COMMAND) -E cmake_progress_start /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/CMakeFiles /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch3/CMakeFiles/progress.marks 84 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug && $(MAKE) -f CMakeFiles/Makefile2 ch3/all 85 | $(CMAKE_COMMAND) -E cmake_progress_start /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/CMakeFiles 0 86 | .PHONY : all 87 | 88 | # The main clean target 89 | clean: 90 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug && $(MAKE) -f CMakeFiles/Makefile2 ch3/clean 91 | .PHONY : clean 92 | 93 | # The main clean target 94 | clean/fast: clean 95 | 96 | .PHONY : clean/fast 97 | 98 | # Prepare targets for installation. 99 | preinstall: all 100 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug && $(MAKE) -f CMakeFiles/Makefile2 ch3/preinstall 101 | .PHONY : preinstall 102 | 103 | # Prepare targets for installation. 104 | preinstall/fast: 105 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug && $(MAKE) -f CMakeFiles/Makefile2 ch3/preinstall 106 | .PHONY : preinstall/fast 107 | 108 | # clear depends 109 | depend: 110 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug && $(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1 111 | .PHONY : depend 112 | 113 | # Help Target 114 | help: 115 | @echo "The following are some of the valid targets for this Makefile:" 116 | @echo "... all (the default if no target is provided)" 117 | @echo "... clean" 118 | @echo "... depend" 119 | @echo "... rebuild_cache" 120 | @echo "... edit_cache" 121 | .PHONY : help 122 | 123 | 124 | 125 | #============================================================================= 126 | # Special targets to cleanup operation of make. 127 | 128 | # Special rule to run CMake to check the build system integrity. 129 | # No rule that depends on this can have commands that come from listfiles 130 | # because they might be regenerated. 131 | cmake_check_build_system: 132 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug && $(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0 133 | .PHONY : cmake_check_build_system 134 | 135 | -------------------------------------------------------------------------------- /cmake-build-debug/ch5/Makefile: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # Default target executed when no arguments are given to make. 5 | default_target: all 6 | 7 | .PHONY : default_target 8 | 9 | # Allow only one "make -f Makefile2" at a time, but pass parallelism. 10 | .NOTPARALLEL: 11 | 12 | 13 | #============================================================================= 14 | # Special targets provided by cmake. 15 | 16 | # Disable implicit rules so canonical targets will work. 17 | .SUFFIXES: 18 | 19 | 20 | # Remove some rules from gmake that .SUFFIXES does not remove. 21 | SUFFIXES = 22 | 23 | .SUFFIXES: .hpux_make_needs_suffix_list 24 | 25 | 26 | # Suppress display of executed commands. 27 | $(VERBOSE).SILENT: 28 | 29 | 30 | # A target that is always out of date. 31 | cmake_force: 32 | 33 | .PHONY : cmake_force 34 | 35 | #============================================================================= 36 | # Set environment variables for the build. 37 | 38 | # The shell in which to execute make rules. 39 | SHELL = /bin/sh 40 | 41 | # The CMake executable. 42 | CMAKE_COMMAND = /home/cmp/Downloads/clion-2019.2/bin/cmake/linux/bin/cmake 43 | 44 | # The command to remove a file. 45 | RM = /home/cmp/Downloads/clion-2019.2/bin/cmake/linux/bin/cmake -E remove -f 46 | 47 | # Escaping for special characters. 48 | EQUALS = = 49 | 50 | # The top-level source directory on which CMake was run. 51 | CMAKE_SOURCE_DIR = /home/cmp/work_dir/source_code/yacc_bison_practice 52 | 53 | # The top-level build directory on which CMake was run. 54 | CMAKE_BINARY_DIR = /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug 55 | 56 | #============================================================================= 57 | # Targets provided globally by CMake. 58 | 59 | # Special rule for the target rebuild_cache 60 | rebuild_cache: 61 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..." 62 | /home/cmp/Downloads/clion-2019.2/bin/cmake/linux/bin/cmake -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) 63 | .PHONY : rebuild_cache 64 | 65 | # Special rule for the target rebuild_cache 66 | rebuild_cache/fast: rebuild_cache 67 | 68 | .PHONY : rebuild_cache/fast 69 | 70 | # Special rule for the target edit_cache 71 | edit_cache: 72 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "No interactive CMake dialog available..." 73 | /home/cmp/Downloads/clion-2019.2/bin/cmake/linux/bin/cmake -E echo No\ interactive\ CMake\ dialog\ available. 74 | .PHONY : edit_cache 75 | 76 | # Special rule for the target edit_cache 77 | edit_cache/fast: edit_cache 78 | 79 | .PHONY : edit_cache/fast 80 | 81 | # The main all target 82 | all: cmake_check_build_system 83 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug && $(CMAKE_COMMAND) -E cmake_progress_start /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/CMakeFiles /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch5/CMakeFiles/progress.marks 84 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug && $(MAKE) -f CMakeFiles/Makefile2 ch5/all 85 | $(CMAKE_COMMAND) -E cmake_progress_start /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/CMakeFiles 0 86 | .PHONY : all 87 | 88 | # The main clean target 89 | clean: 90 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug && $(MAKE) -f CMakeFiles/Makefile2 ch5/clean 91 | .PHONY : clean 92 | 93 | # The main clean target 94 | clean/fast: clean 95 | 96 | .PHONY : clean/fast 97 | 98 | # Prepare targets for installation. 99 | preinstall: all 100 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug && $(MAKE) -f CMakeFiles/Makefile2 ch5/preinstall 101 | .PHONY : preinstall 102 | 103 | # Prepare targets for installation. 104 | preinstall/fast: 105 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug && $(MAKE) -f CMakeFiles/Makefile2 ch5/preinstall 106 | .PHONY : preinstall/fast 107 | 108 | # clear depends 109 | depend: 110 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug && $(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1 111 | .PHONY : depend 112 | 113 | # Help Target 114 | help: 115 | @echo "The following are some of the valid targets for this Makefile:" 116 | @echo "... all (the default if no target is provided)" 117 | @echo "... clean" 118 | @echo "... depend" 119 | @echo "... rebuild_cache" 120 | @echo "... edit_cache" 121 | .PHONY : help 122 | 123 | 124 | 125 | #============================================================================= 126 | # Special targets to cleanup operation of make. 127 | 128 | # Special rule to run CMake to check the build system integrity. 129 | # No rule that depends on this can have commands that come from listfiles 130 | # because they might be regenerated. 131 | cmake_check_build_system: 132 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug && $(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0 133 | .PHONY : cmake_check_build_system 134 | 135 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.03_read_files.dir/build.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # Delete rule output on recipe failure. 5 | .DELETE_ON_ERROR: 6 | 7 | 8 | #============================================================================= 9 | # Special targets provided by cmake. 10 | 11 | # Disable implicit rules so canonical targets will work. 12 | .SUFFIXES: 13 | 14 | 15 | # Remove some rules from gmake that .SUFFIXES does not remove. 16 | SUFFIXES = 17 | 18 | .SUFFIXES: .hpux_make_needs_suffix_list 19 | 20 | 21 | # Suppress display of executed commands. 22 | $(VERBOSE).SILENT: 23 | 24 | 25 | # A target that is always out of date. 26 | cmake_force: 27 | 28 | .PHONY : cmake_force 29 | 30 | #============================================================================= 31 | # Set environment variables for the build. 32 | 33 | # The shell in which to execute make rules. 34 | SHELL = /bin/sh 35 | 36 | # The CMake executable. 37 | CMAKE_COMMAND = /home/cmp/Downloads/clion-2019.2/bin/cmake/linux/bin/cmake 38 | 39 | # The command to remove a file. 40 | RM = /home/cmp/Downloads/clion-2019.2/bin/cmake/linux/bin/cmake -E remove -f 41 | 42 | # Escaping for special characters. 43 | EQUALS = = 44 | 45 | # The top-level source directory on which CMake was run. 46 | CMAKE_SOURCE_DIR = /home/cmp/work_dir/source_code/yacc_bison_practice 47 | 48 | # The top-level build directory on which CMake was run. 49 | CMAKE_BINARY_DIR = /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug 50 | 51 | # Include any dependencies generated for this target. 52 | include ch2/CMakeFiles/2.03_read_files.dir/depend.make 53 | 54 | # Include the progress variables for this target. 55 | include ch2/CMakeFiles/2.03_read_files.dir/progress.make 56 | 57 | # Include the compile flags for this target's objects. 58 | include ch2/CMakeFiles/2.03_read_files.dir/flags.make 59 | 60 | ch2/CMakeFiles/2.03_read_files.dir/2.03_read_files.c.o: ch2/CMakeFiles/2.03_read_files.dir/flags.make 61 | ch2/CMakeFiles/2.03_read_files.dir/2.03_read_files.c.o: ch2/2.03_read_files.c 62 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --progress-dir=/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/CMakeFiles --progress-num=$(CMAKE_PROGRESS_1) "Building C object ch2/CMakeFiles/2.03_read_files.dir/2.03_read_files.c.o" 63 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2 && /usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -o CMakeFiles/2.03_read_files.dir/2.03_read_files.c.o -c /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/2.03_read_files.c 64 | 65 | ch2/CMakeFiles/2.03_read_files.dir/2.03_read_files.c.i: cmake_force 66 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Preprocessing C source to CMakeFiles/2.03_read_files.dir/2.03_read_files.c.i" 67 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2 && /usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -E /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/2.03_read_files.c > CMakeFiles/2.03_read_files.dir/2.03_read_files.c.i 68 | 69 | ch2/CMakeFiles/2.03_read_files.dir/2.03_read_files.c.s: cmake_force 70 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Compiling C source to assembly CMakeFiles/2.03_read_files.dir/2.03_read_files.c.s" 71 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2 && /usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -S /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/2.03_read_files.c -o CMakeFiles/2.03_read_files.dir/2.03_read_files.c.s 72 | 73 | # Object files for target 2.03_read_files 74 | 2_03_read_files_OBJECTS = \ 75 | "CMakeFiles/2.03_read_files.dir/2.03_read_files.c.o" 76 | 77 | # External object files for target 2.03_read_files 78 | 2_03_read_files_EXTERNAL_OBJECTS = 79 | 80 | ch2/2.03_read_files: ch2/CMakeFiles/2.03_read_files.dir/2.03_read_files.c.o 81 | ch2/2.03_read_files: ch2/CMakeFiles/2.03_read_files.dir/build.make 82 | ch2/2.03_read_files: ch2/CMakeFiles/2.03_read_files.dir/link.txt 83 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --bold --progress-dir=/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/CMakeFiles --progress-num=$(CMAKE_PROGRESS_2) "Linking C executable 2.03_read_files" 84 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2 && $(CMAKE_COMMAND) -E cmake_link_script CMakeFiles/2.03_read_files.dir/link.txt --verbose=$(VERBOSE) 85 | 86 | # Rule to build all files generated by this target. 87 | ch2/CMakeFiles/2.03_read_files.dir/build: ch2/2.03_read_files 88 | 89 | .PHONY : ch2/CMakeFiles/2.03_read_files.dir/build 90 | 91 | ch2/CMakeFiles/2.03_read_files.dir/clean: 92 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2 && $(CMAKE_COMMAND) -P CMakeFiles/2.03_read_files.dir/cmake_clean.cmake 93 | .PHONY : ch2/CMakeFiles/2.03_read_files.dir/clean 94 | 95 | ch2/CMakeFiles/2.03_read_files.dir/depend: 96 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug && $(CMAKE_COMMAND) -E cmake_depends "Unix Makefiles" /home/cmp/work_dir/source_code/yacc_bison_practice /home/cmp/work_dir/source_code/yacc_bison_practice/ch2 /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2 /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/CMakeFiles/2.03_read_files.dir/DependInfo.cmake --color=$(COLOR) 97 | .PHONY : ch2/CMakeFiles/2.03_read_files.dir/depend 98 | 99 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.01_word_counter.dir/build.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # Delete rule output on recipe failure. 5 | .DELETE_ON_ERROR: 6 | 7 | 8 | #============================================================================= 9 | # Special targets provided by cmake. 10 | 11 | # Disable implicit rules so canonical targets will work. 12 | .SUFFIXES: 13 | 14 | 15 | # Remove some rules from gmake that .SUFFIXES does not remove. 16 | SUFFIXES = 17 | 18 | .SUFFIXES: .hpux_make_needs_suffix_list 19 | 20 | 21 | # Suppress display of executed commands. 22 | $(VERBOSE).SILENT: 23 | 24 | 25 | # A target that is always out of date. 26 | cmake_force: 27 | 28 | .PHONY : cmake_force 29 | 30 | #============================================================================= 31 | # Set environment variables for the build. 32 | 33 | # The shell in which to execute make rules. 34 | SHELL = /bin/sh 35 | 36 | # The CMake executable. 37 | CMAKE_COMMAND = /home/cmp/Downloads/clion-2019.2/bin/cmake/linux/bin/cmake 38 | 39 | # The command to remove a file. 40 | RM = /home/cmp/Downloads/clion-2019.2/bin/cmake/linux/bin/cmake -E remove -f 41 | 42 | # Escaping for special characters. 43 | EQUALS = = 44 | 45 | # The top-level source directory on which CMake was run. 46 | CMAKE_SOURCE_DIR = /home/cmp/work_dir/source_code/yacc_bison_practice 47 | 48 | # The top-level build directory on which CMake was run. 49 | CMAKE_BINARY_DIR = /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug 50 | 51 | # Include any dependencies generated for this target. 52 | include ch1/CMakeFiles/1.01_word_counter.dir/depend.make 53 | 54 | # Include the progress variables for this target. 55 | include ch1/CMakeFiles/1.01_word_counter.dir/progress.make 56 | 57 | # Include the compile flags for this target's objects. 58 | include ch1/CMakeFiles/1.01_word_counter.dir/flags.make 59 | 60 | ch1/CMakeFiles/1.01_word_counter.dir/1.01_word_counter.c.o: ch1/CMakeFiles/1.01_word_counter.dir/flags.make 61 | ch1/CMakeFiles/1.01_word_counter.dir/1.01_word_counter.c.o: ch1/1.01_word_counter.c 62 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --progress-dir=/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/CMakeFiles --progress-num=$(CMAKE_PROGRESS_1) "Building C object ch1/CMakeFiles/1.01_word_counter.dir/1.01_word_counter.c.o" 63 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1 && /usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -o CMakeFiles/1.01_word_counter.dir/1.01_word_counter.c.o -c /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/1.01_word_counter.c 64 | 65 | ch1/CMakeFiles/1.01_word_counter.dir/1.01_word_counter.c.i: cmake_force 66 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Preprocessing C source to CMakeFiles/1.01_word_counter.dir/1.01_word_counter.c.i" 67 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1 && /usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -E /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/1.01_word_counter.c > CMakeFiles/1.01_word_counter.dir/1.01_word_counter.c.i 68 | 69 | ch1/CMakeFiles/1.01_word_counter.dir/1.01_word_counter.c.s: cmake_force 70 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Compiling C source to assembly CMakeFiles/1.01_word_counter.dir/1.01_word_counter.c.s" 71 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1 && /usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -S /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/1.01_word_counter.c -o CMakeFiles/1.01_word_counter.dir/1.01_word_counter.c.s 72 | 73 | # Object files for target 1.01_word_counter 74 | 1_01_word_counter_OBJECTS = \ 75 | "CMakeFiles/1.01_word_counter.dir/1.01_word_counter.c.o" 76 | 77 | # External object files for target 1.01_word_counter 78 | 1_01_word_counter_EXTERNAL_OBJECTS = 79 | 80 | ch1/1.01_word_counter: ch1/CMakeFiles/1.01_word_counter.dir/1.01_word_counter.c.o 81 | ch1/1.01_word_counter: ch1/CMakeFiles/1.01_word_counter.dir/build.make 82 | ch1/1.01_word_counter: ch1/CMakeFiles/1.01_word_counter.dir/link.txt 83 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --bold --progress-dir=/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/CMakeFiles --progress-num=$(CMAKE_PROGRESS_2) "Linking C executable 1.01_word_counter" 84 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1 && $(CMAKE_COMMAND) -E cmake_link_script CMakeFiles/1.01_word_counter.dir/link.txt --verbose=$(VERBOSE) 85 | 86 | # Rule to build all files generated by this target. 87 | ch1/CMakeFiles/1.01_word_counter.dir/build: ch1/1.01_word_counter 88 | 89 | .PHONY : ch1/CMakeFiles/1.01_word_counter.dir/build 90 | 91 | ch1/CMakeFiles/1.01_word_counter.dir/clean: 92 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1 && $(CMAKE_COMMAND) -P CMakeFiles/1.01_word_counter.dir/cmake_clean.cmake 93 | .PHONY : ch1/CMakeFiles/1.01_word_counter.dir/clean 94 | 95 | ch1/CMakeFiles/1.01_word_counter.dir/depend: 96 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug && $(CMAKE_COMMAND) -E cmake_depends "Unix Makefiles" /home/cmp/work_dir/source_code/yacc_bison_practice /home/cmp/work_dir/source_code/yacc_bison_practice/ch1 /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1 /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/CMakeFiles/1.01_word_counter.dir/DependInfo.cmake --color=$(COLOR) 97 | .PHONY : ch1/CMakeFiles/1.01_word_counter.dir/depend 98 | 99 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/build.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # Delete rule output on recipe failure. 5 | .DELETE_ON_ERROR: 6 | 7 | 8 | #============================================================================= 9 | # Special targets provided by cmake. 10 | 11 | # Disable implicit rules so canonical targets will work. 12 | .SUFFIXES: 13 | 14 | 15 | # Remove some rules from gmake that .SUFFIXES does not remove. 16 | SUFFIXES = 17 | 18 | .SUFFIXES: .hpux_make_needs_suffix_list 19 | 20 | 21 | # Suppress display of executed commands. 22 | $(VERBOSE).SILENT: 23 | 24 | 25 | # A target that is always out of date. 26 | cmake_force: 27 | 28 | .PHONY : cmake_force 29 | 30 | #============================================================================= 31 | # Set environment variables for the build. 32 | 33 | # The shell in which to execute make rules. 34 | SHELL = /bin/sh 35 | 36 | # The CMake executable. 37 | CMAKE_COMMAND = /home/cmp/Downloads/clion-2019.2/bin/cmake/linux/bin/cmake 38 | 39 | # The command to remove a file. 40 | RM = /home/cmp/Downloads/clion-2019.2/bin/cmake/linux/bin/cmake -E remove -f 41 | 42 | # Escaping for special characters. 43 | EQUALS = = 44 | 45 | # The top-level source directory on which CMake was run. 46 | CMAKE_SOURCE_DIR = /home/cmp/work_dir/source_code/yacc_bison_practice 47 | 48 | # The top-level build directory on which CMake was run. 49 | CMAKE_BINARY_DIR = /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug 50 | 51 | # Include any dependencies generated for this target. 52 | include ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/depend.make 53 | 54 | # Include the progress variables for this target. 55 | include ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/progress.make 56 | 57 | # Include the compile flags for this target's objects. 58 | include ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/flags.make 59 | 60 | ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/1.04_main.c.o: ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/flags.make 61 | ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/1.04_main.c.o: ../ch1/1.04_main.c 62 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --progress-dir=/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/CMakeFiles --progress-num=$(CMAKE_PROGRESS_1) "Building C object ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/1.04_main.c.o" 63 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1 && /usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -o CMakeFiles/1.04_NextToken_with_Flex.dir/1.04_main.c.o -c /home/cmp/work_dir/source_code/yacc_bison_practice/ch1/1.04_main.c 64 | 65 | ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/1.04_main.c.i: cmake_force 66 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Preprocessing C source to CMakeFiles/1.04_NextToken_with_Flex.dir/1.04_main.c.i" 67 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1 && /usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -E /home/cmp/work_dir/source_code/yacc_bison_practice/ch1/1.04_main.c > CMakeFiles/1.04_NextToken_with_Flex.dir/1.04_main.c.i 68 | 69 | ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/1.04_main.c.s: cmake_force 70 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Compiling C source to assembly CMakeFiles/1.04_NextToken_with_Flex.dir/1.04_main.c.s" 71 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1 && /usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -S /home/cmp/work_dir/source_code/yacc_bison_practice/ch1/1.04_main.c -o CMakeFiles/1.04_NextToken_with_Flex.dir/1.04_main.c.s 72 | 73 | # Object files for target 1.04_NextToken_with_Flex 74 | 1_04_NextToken_with_Flex_OBJECTS = \ 75 | "CMakeFiles/1.04_NextToken_with_Flex.dir/1.04_main.c.o" 76 | 77 | # External object files for target 1.04_NextToken_with_Flex 78 | 1_04_NextToken_with_Flex_EXTERNAL_OBJECTS = 79 | 80 | ch1/1.04_NextToken_with_Flex: ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/1.04_main.c.o 81 | ch1/1.04_NextToken_with_Flex: ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/build.make 82 | ch1/1.04_NextToken_with_Flex: ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/link.txt 83 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --bold --progress-dir=/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/CMakeFiles --progress-num=$(CMAKE_PROGRESS_2) "Linking C executable 1.04_NextToken_with_Flex" 84 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1 && $(CMAKE_COMMAND) -E cmake_link_script CMakeFiles/1.04_NextToken_with_Flex.dir/link.txt --verbose=$(VERBOSE) 85 | 86 | # Rule to build all files generated by this target. 87 | ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/build: ch1/1.04_NextToken_with_Flex 88 | 89 | .PHONY : ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/build 90 | 91 | ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/clean: 92 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1 && $(CMAKE_COMMAND) -P CMakeFiles/1.04_NextToken_with_Flex.dir/cmake_clean.cmake 93 | .PHONY : ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/clean 94 | 95 | ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/depend: 96 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug && $(CMAKE_COMMAND) -E cmake_depends "Unix Makefiles" /home/cmp/work_dir/source_code/yacc_bison_practice /home/cmp/work_dir/source_code/yacc_bison_practice/ch1 /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1 /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/DependInfo.cmake --color=$(COLOR) 97 | .PHONY : ch1/CMakeFiles/1.04_NextToken_with_Flex.dir/depend 98 | 99 | -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/3.16.5/CMakeCXXCompiler.cmake: -------------------------------------------------------------------------------- 1 | set(CMAKE_CXX_COMPILER "/usr/bin/c++") 2 | set(CMAKE_CXX_COMPILER_ARG1 "") 3 | set(CMAKE_CXX_COMPILER_ID "GNU") 4 | set(CMAKE_CXX_COMPILER_VERSION "7.4.0") 5 | set(CMAKE_CXX_COMPILER_VERSION_INTERNAL "") 6 | set(CMAKE_CXX_COMPILER_WRAPPER "") 7 | set(CMAKE_CXX_STANDARD_COMPUTED_DEFAULT "14") 8 | set(CMAKE_CXX_COMPILE_FEATURES "cxx_std_98;cxx_template_template_parameters;cxx_std_11;cxx_alias_templates;cxx_alignas;cxx_alignof;cxx_attributes;cxx_auto_type;cxx_constexpr;cxx_decltype;cxx_decltype_incomplete_return_types;cxx_default_function_template_args;cxx_defaulted_functions;cxx_defaulted_move_initializers;cxx_delegating_constructors;cxx_deleted_functions;cxx_enum_forward_declarations;cxx_explicit_conversions;cxx_extended_friend_declarations;cxx_extern_templates;cxx_final;cxx_func_identifier;cxx_generalized_initializers;cxx_inheriting_constructors;cxx_inline_namespaces;cxx_lambdas;cxx_local_type_template_args;cxx_long_long_type;cxx_noexcept;cxx_nonstatic_member_init;cxx_nullptr;cxx_override;cxx_range_for;cxx_raw_string_literals;cxx_reference_qualified_functions;cxx_right_angle_brackets;cxx_rvalue_references;cxx_sizeof_member;cxx_static_assert;cxx_strong_enums;cxx_thread_local;cxx_trailing_return_types;cxx_unicode_literals;cxx_uniform_initialization;cxx_unrestricted_unions;cxx_user_literals;cxx_variadic_macros;cxx_variadic_templates;cxx_std_14;cxx_aggregate_default_initializers;cxx_attribute_deprecated;cxx_binary_literals;cxx_contextual_conversions;cxx_decltype_auto;cxx_digit_separators;cxx_generic_lambdas;cxx_lambda_init_captures;cxx_relaxed_constexpr;cxx_return_type_deduction;cxx_variable_templates;cxx_std_17") 9 | set(CMAKE_CXX98_COMPILE_FEATURES "cxx_std_98;cxx_template_template_parameters") 10 | set(CMAKE_CXX11_COMPILE_FEATURES "cxx_std_11;cxx_alias_templates;cxx_alignas;cxx_alignof;cxx_attributes;cxx_auto_type;cxx_constexpr;cxx_decltype;cxx_decltype_incomplete_return_types;cxx_default_function_template_args;cxx_defaulted_functions;cxx_defaulted_move_initializers;cxx_delegating_constructors;cxx_deleted_functions;cxx_enum_forward_declarations;cxx_explicit_conversions;cxx_extended_friend_declarations;cxx_extern_templates;cxx_final;cxx_func_identifier;cxx_generalized_initializers;cxx_inheriting_constructors;cxx_inline_namespaces;cxx_lambdas;cxx_local_type_template_args;cxx_long_long_type;cxx_noexcept;cxx_nonstatic_member_init;cxx_nullptr;cxx_override;cxx_range_for;cxx_raw_string_literals;cxx_reference_qualified_functions;cxx_right_angle_brackets;cxx_rvalue_references;cxx_sizeof_member;cxx_static_assert;cxx_strong_enums;cxx_thread_local;cxx_trailing_return_types;cxx_unicode_literals;cxx_uniform_initialization;cxx_unrestricted_unions;cxx_user_literals;cxx_variadic_macros;cxx_variadic_templates") 11 | set(CMAKE_CXX14_COMPILE_FEATURES "cxx_std_14;cxx_aggregate_default_initializers;cxx_attribute_deprecated;cxx_binary_literals;cxx_contextual_conversions;cxx_decltype_auto;cxx_digit_separators;cxx_generic_lambdas;cxx_lambda_init_captures;cxx_relaxed_constexpr;cxx_return_type_deduction;cxx_variable_templates") 12 | set(CMAKE_CXX17_COMPILE_FEATURES "cxx_std_17") 13 | set(CMAKE_CXX20_COMPILE_FEATURES "") 14 | 15 | set(CMAKE_CXX_PLATFORM_ID "Linux") 16 | set(CMAKE_CXX_SIMULATE_ID "") 17 | set(CMAKE_CXX_COMPILER_FRONTEND_VARIANT "") 18 | set(CMAKE_CXX_SIMULATE_VERSION "") 19 | 20 | 21 | 22 | set(CMAKE_AR "/usr/bin/ar") 23 | set(CMAKE_CXX_COMPILER_AR "/usr/bin/gcc-ar-7") 24 | set(CMAKE_RANLIB "/usr/bin/ranlib") 25 | set(CMAKE_CXX_COMPILER_RANLIB "/usr/bin/gcc-ranlib-7") 26 | set(CMAKE_LINKER "/usr/bin/ld") 27 | set(CMAKE_MT "") 28 | set(CMAKE_COMPILER_IS_GNUCXX 1) 29 | set(CMAKE_CXX_COMPILER_LOADED 1) 30 | set(CMAKE_CXX_COMPILER_WORKS TRUE) 31 | set(CMAKE_CXX_ABI_COMPILED TRUE) 32 | set(CMAKE_COMPILER_IS_MINGW ) 33 | set(CMAKE_COMPILER_IS_CYGWIN ) 34 | if(CMAKE_COMPILER_IS_CYGWIN) 35 | set(CYGWIN 1) 36 | set(UNIX 1) 37 | endif() 38 | 39 | set(CMAKE_CXX_COMPILER_ENV_VAR "CXX") 40 | 41 | if(CMAKE_COMPILER_IS_MINGW) 42 | set(MINGW 1) 43 | endif() 44 | set(CMAKE_CXX_COMPILER_ID_RUN 1) 45 | set(CMAKE_CXX_SOURCE_FILE_EXTENSIONS C;M;c++;cc;cpp;cxx;m;mm;CPP) 46 | set(CMAKE_CXX_IGNORE_EXTENSIONS inl;h;hpp;HPP;H;o;O;obj;OBJ;def;DEF;rc;RC) 47 | 48 | foreach (lang C OBJC OBJCXX) 49 | if (CMAKE_${lang}_COMPILER_ID_RUN) 50 | foreach(extension IN LISTS CMAKE_${lang}_SOURCE_FILE_EXTENSIONS) 51 | list(REMOVE_ITEM CMAKE_CXX_SOURCE_FILE_EXTENSIONS ${extension}) 52 | endforeach() 53 | endif() 54 | endforeach() 55 | 56 | set(CMAKE_CXX_LINKER_PREFERENCE 30) 57 | set(CMAKE_CXX_LINKER_PREFERENCE_PROPAGATES 1) 58 | 59 | # Save compiler ABI information. 60 | set(CMAKE_CXX_SIZEOF_DATA_PTR "8") 61 | set(CMAKE_CXX_COMPILER_ABI "ELF") 62 | set(CMAKE_CXX_LIBRARY_ARCHITECTURE "x86_64-linux-gnu") 63 | 64 | if(CMAKE_CXX_SIZEOF_DATA_PTR) 65 | set(CMAKE_SIZEOF_VOID_P "${CMAKE_CXX_SIZEOF_DATA_PTR}") 66 | endif() 67 | 68 | if(CMAKE_CXX_COMPILER_ABI) 69 | set(CMAKE_INTERNAL_PLATFORM_ABI "${CMAKE_CXX_COMPILER_ABI}") 70 | endif() 71 | 72 | if(CMAKE_CXX_LIBRARY_ARCHITECTURE) 73 | set(CMAKE_LIBRARY_ARCHITECTURE "x86_64-linux-gnu") 74 | endif() 75 | 76 | set(CMAKE_CXX_CL_SHOWINCLUDES_PREFIX "") 77 | if(CMAKE_CXX_CL_SHOWINCLUDES_PREFIX) 78 | set(CMAKE_CL_SHOWINCLUDES_PREFIX "${CMAKE_CXX_CL_SHOWINCLUDES_PREFIX}") 79 | endif() 80 | 81 | 82 | 83 | 84 | 85 | set(CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES "/usr/include/c++/7;/usr/include/x86_64-linux-gnu/c++/7;/usr/include/c++/7/backward;/usr/lib/gcc/x86_64-linux-gnu/7/include;/usr/local/include;/usr/lib/gcc/x86_64-linux-gnu/7/include-fixed;/usr/include/x86_64-linux-gnu;/usr/include") 86 | set(CMAKE_CXX_IMPLICIT_LINK_LIBRARIES "stdc++;m;gcc_s;gcc;c;gcc_s;gcc") 87 | set(CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES "/usr/lib/gcc/x86_64-linux-gnu/7;/usr/lib/x86_64-linux-gnu;/usr/lib;/lib/x86_64-linux-gnu;/lib") 88 | set(CMAKE_CXX_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "") 89 | -------------------------------------------------------------------------------- /cmake-build-debug/ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/build.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # Delete rule output on recipe failure. 5 | .DELETE_ON_ERROR: 6 | 7 | 8 | #============================================================================= 9 | # Special targets provided by cmake. 10 | 11 | # Disable implicit rules so canonical targets will work. 12 | .SUFFIXES: 13 | 14 | 15 | # Remove some rules from gmake that .SUFFIXES does not remove. 16 | SUFFIXES = 17 | 18 | .SUFFIXES: .hpux_make_needs_suffix_list 19 | 20 | 21 | # Suppress display of executed commands. 22 | $(VERBOSE).SILENT: 23 | 24 | 25 | # A target that is always out of date. 26 | cmake_force: 27 | 28 | .PHONY : cmake_force 29 | 30 | #============================================================================= 31 | # Set environment variables for the build. 32 | 33 | # The shell in which to execute make rules. 34 | SHELL = /bin/sh 35 | 36 | # The CMake executable. 37 | CMAKE_COMMAND = /home/cmp/Downloads/clion-2019.2/bin/cmake/linux/bin/cmake 38 | 39 | # The command to remove a file. 40 | RM = /home/cmp/Downloads/clion-2019.2/bin/cmake/linux/bin/cmake -E remove -f 41 | 42 | # Escaping for special characters. 43 | EQUALS = = 44 | 45 | # The top-level source directory on which CMake was run. 46 | CMAKE_SOURCE_DIR = /home/cmp/work_dir/source_code/yacc_bison_practice 47 | 48 | # The top-level build directory on which CMake was run. 49 | CMAKE_BINARY_DIR = /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug 50 | 51 | # Include any dependencies generated for this target. 52 | include ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/depend.make 53 | 54 | # Include the progress variables for this target. 55 | include ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/progress.make 56 | 57 | # Include the compile flags for this target's objects. 58 | include ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/flags.make 59 | 60 | ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/2.01_usual_lang_lex_re.c.o: ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/flags.make 61 | ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/2.01_usual_lang_lex_re.c.o: ch2/2.01_usual_lang_lex_re.c 62 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --progress-dir=/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/CMakeFiles --progress-num=$(CMAKE_PROGRESS_1) "Building C object ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/2.01_usual_lang_lex_re.c.o" 63 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2 && /usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -o CMakeFiles/2.01_usual_lang_lex_re.dir/2.01_usual_lang_lex_re.c.o -c /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/2.01_usual_lang_lex_re.c 64 | 65 | ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/2.01_usual_lang_lex_re.c.i: cmake_force 66 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Preprocessing C source to CMakeFiles/2.01_usual_lang_lex_re.dir/2.01_usual_lang_lex_re.c.i" 67 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2 && /usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -E /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/2.01_usual_lang_lex_re.c > CMakeFiles/2.01_usual_lang_lex_re.dir/2.01_usual_lang_lex_re.c.i 68 | 69 | ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/2.01_usual_lang_lex_re.c.s: cmake_force 70 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Compiling C source to assembly CMakeFiles/2.01_usual_lang_lex_re.dir/2.01_usual_lang_lex_re.c.s" 71 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2 && /usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -S /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/2.01_usual_lang_lex_re.c -o CMakeFiles/2.01_usual_lang_lex_re.dir/2.01_usual_lang_lex_re.c.s 72 | 73 | # Object files for target 2.01_usual_lang_lex_re 74 | 2_01_usual_lang_lex_re_OBJECTS = \ 75 | "CMakeFiles/2.01_usual_lang_lex_re.dir/2.01_usual_lang_lex_re.c.o" 76 | 77 | # External object files for target 2.01_usual_lang_lex_re 78 | 2_01_usual_lang_lex_re_EXTERNAL_OBJECTS = 79 | 80 | ch2/2.01_usual_lang_lex_re: ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/2.01_usual_lang_lex_re.c.o 81 | ch2/2.01_usual_lang_lex_re: ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/build.make 82 | ch2/2.01_usual_lang_lex_re: ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/link.txt 83 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --bold --progress-dir=/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/CMakeFiles --progress-num=$(CMAKE_PROGRESS_2) "Linking C executable 2.01_usual_lang_lex_re" 84 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2 && $(CMAKE_COMMAND) -E cmake_link_script CMakeFiles/2.01_usual_lang_lex_re.dir/link.txt --verbose=$(VERBOSE) 85 | 86 | # Rule to build all files generated by this target. 87 | ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/build: ch2/2.01_usual_lang_lex_re 88 | 89 | .PHONY : ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/build 90 | 91 | ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/clean: 92 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2 && $(CMAKE_COMMAND) -P CMakeFiles/2.01_usual_lang_lex_re.dir/cmake_clean.cmake 93 | .PHONY : ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/clean 94 | 95 | ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/depend: 96 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug && $(CMAKE_COMMAND) -E cmake_depends "Unix Makefiles" /home/cmp/work_dir/source_code/yacc_bison_practice /home/cmp/work_dir/source_code/yacc_bison_practice/ch2 /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2 /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/DependInfo.cmake --color=$(COLOR) 97 | .PHONY : ch2/CMakeFiles/2.01_usual_lang_lex_re.dir/depend 98 | 99 | -------------------------------------------------------------------------------- /ch1/1.04_NextToken_with_Flex.l: -------------------------------------------------------------------------------- 1 | /* 2 | // file: 1.04_NextToken_with_Flex.y 3 | 在词法分析的时候,经常我们需要一个NextToken的函数,这个函数每一次调用返回下一个识别了的Token 4 | 本例子就是结合Flex来实现NextToken函数的基本功能,来识别C语言中的token 5 | 该词法分析器仅支持标准C98的关键字识别,新的标准增加的关键字并不支持 6 | 此外,对于递增操作符++、递减操作符--、正号+、负号-,不应该放在词法分析器中识别,它们属于语法的范畴。应该在语法分析中识别和处理 7 | */ 8 | 9 | %{ 10 | enum TokenType 11 | { 12 | INTEGER = 1001, // 整数,123 13 | DECIMAL = 1002, // 小数,123.554 14 | IDENTIFIER = 1003, // 变量,adfa 15 | OPERATOR = 1004, // 操作符,+/* 16 | KEYWORD = 1005, // 关键字,if/for 17 | L_PARENTHESIS = 1006, // 左圆括号,( 18 | R_PARENTHESIS = 1007, // 右圆括号,) 19 | L_SQUARE_BRACKET = 1008, // 左方括号,[ 20 | R_SQUARE_BRACKET = 1009, // 右方括号,] 21 | L_BRACE = 1010, // 左大括号,{ 22 | R_BRACE = 1011, // 右大括号,} 23 | QUESTION_SIGN = 1012, // 问号,? 24 | COMMA = 1013, // 逗号,, 25 | COLON = 1014, // 冒号,: 26 | SEMICOLON = 1015, // 分号,; 27 | POUND_SIGN = 1016, // 井号,# 28 | LESS_THAN = 1017, // 小于,< 29 | LESS_EQUAL_THAN = 1018, // 小于等于,<= 30 | GREAT_THAN = 1019, // 大于,> 31 | GREAT_EQUAL_THAN = 1020, // 大于等于,>= 32 | 33 | QUOTATION = 1021, // 单引号,' 34 | 35 | STRING = 1022, // 字符串, 36 | ANNOTATION = 1023, // 单行注释," 37 | MULTI_LINE_ANNOTATION = 1024, // 多行注释," 38 | 39 | }; 40 | int yylval = 0; 41 | %} 42 | 43 | %% 44 | [*/+-] { return OPERATOR;} // 四则运算符 +-*/ 45 | "%" { return OPERATOR;} // 取模运符 % 46 | "**" { return OPERATOR;} // 乘方运符 ** 47 | [~|&^] { return OPERATOR;} // 位运算符 ~|&^ 48 | ">>" { return OPERATOR;} // 位运算符 >> 49 | "<<" { return OPERATOR;} // 位运算符 << 50 | "&&" { return OPERATOR;} // 布尔运符 && 51 | "||" { return OPERATOR;} // 布尔运符 || 52 | "!" { return OPERATOR;} // 布尔运符 ! 53 | "->" { return OPERATOR;} // 成员运算符号 54 | 55 | "[" { return L_SQUARE_BRACKET; } 56 | "]" { return R_SQUARE_BRACKET; } 57 | 58 | "(" { return L_PARENTHESIS; } 59 | ")" { return R_PARENTHESIS; } 60 | 61 | "{" { return L_BRACE; } 62 | "}" { return R_BRACE; } 63 | 64 | "<" { return LESS_THAN; } 65 | "<=" { return LESS_EQUAL_THAN; } 66 | ">" { return GREAT_THAN; } 67 | ">=" { return GREAT_EQUAL_THAN; } 68 | 69 | "?" { return QUESTION_SIGN; } 70 | "," { return COMMA; } 71 | ":" { return COLON; } 72 | ";" { return SEMICOLON; } 73 | "#" { return POUND_SIGN; } 74 | "'" { return QUOTATION; } 75 | 76 | 77 | "auto" { return KEYWORD; } 78 | "break" { return KEYWORD; } 79 | "case" { return KEYWORD; } 80 | "char" { return KEYWORD; } 81 | "const" { return KEYWORD; } 82 | "continue" { return KEYWORD; } 83 | "default" { return KEYWORD; } 84 | "do" { return KEYWORD; } 85 | "double" { return KEYWORD; } 86 | "else" { return KEYWORD; } 87 | "enum" { return KEYWORD; } 88 | "extern" { return KEYWORD; } 89 | "float" { return KEYWORD; } 90 | "for" { return KEYWORD; } 91 | "goto" { return KEYWORD; } 92 | "if" { return KEYWORD; } 93 | "int" { return KEYWORD; } 94 | "long" { return KEYWORD; } 95 | "register" { return KEYWORD; } 96 | "return" { return KEYWORD; } 97 | "short" { return KEYWORD; } 98 | "signed" { return KEYWORD; } 99 | "sizeof" { return KEYWORD; } 100 | "static" { return KEYWORD; } 101 | "struct" { return KEYWORD; } 102 | "switch" { return KEYWORD; } 103 | "typedef" { return KEYWORD; } 104 | "union" { return KEYWORD; } 105 | "unsigned" { return KEYWORD; } 106 | "void" { return KEYWORD; } 107 | "volatile" { return KEYWORD; } 108 | "while" { return KEYWORD; } 109 | 110 | 111 | [0-9]+ { return INTEGER; } // 识别整数。为什么不用"[-+]?[0-9]+"这样的规则去识别一个整数,是因为"-"是一个语义相关的。例如:"-2"在表达式"1-2"中应该识别为减号和整数,而在"1--2"中"-2"应该识别为一个负整数 112 | [a-zA-Z_][a-zA-Z0-9_]* { return IDENTIFIER; } // 识别标识符 113 | (([0-9]*\.?[0-9]+)|([0-9]+\.[0-9]*))(E[+-]?[0-9]+)? { return DECIMAL; } // 识别小数,支持小数的科学计数法识别。为什么不用"[-+]?(([0-9]*\.?[0-9]+)|([0-9]+\.[0-9]*))(E[+-]?[0-9]+)?"这样的规则去识别一个整数,是因为"-"是一个语义相关的。 114 | 115 | \"[^"]*\" { return STRING; } // 识别字符串 116 | 117 | "//"[^\n]*\n { return ANNOTATION; } // 识别单行注释 118 | "/*"([^*]|\*+[^/*])*"*/" { return MULTI_LINE_ANNOTATION; } // 识别多行注释 119 | 120 | [ \t] { /*忽略空白字符*/ } 121 | %% 122 | 123 | int NextToken() 124 | { 125 | /* 126 | 每当调用yylex()时,它都会从全局输入文件yyin(默认为stdin)中扫描token。 127 | 它一直持续到到达文件结尾(此时它返回值0)或它的其中一个动作执行return语句为止。 128 | */ 129 | return yylex(); 130 | }; 131 | 132 | 133 | 134 | //注意: 135 | // 对于识别多行注释: 136 | // "/*"([^*]|\*+[^/*])*"*/" { return MULTI_LINE_ANNOTATION; } // 识别多行注释 137 | // 是不对的,虽然能够满足大多的场景。 但有可能注释会很长,而flex的记号有一定的输入缓冲的长度限制,通常是16K。 138 | // 为了能够满足对注释的正确识别,应该使用起始条件(start condition)方法。 139 | 140 | 141 | -------------------------------------------------------------------------------- /cmake-build-debug/ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/build.make: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.16 3 | 4 | # Delete rule output on recipe failure. 5 | .DELETE_ON_ERROR: 6 | 7 | 8 | #============================================================================= 9 | # Special targets provided by cmake. 10 | 11 | # Disable implicit rules so canonical targets will work. 12 | .SUFFIXES: 13 | 14 | 15 | # Remove some rules from gmake that .SUFFIXES does not remove. 16 | SUFFIXES = 17 | 18 | .SUFFIXES: .hpux_make_needs_suffix_list 19 | 20 | 21 | # Suppress display of executed commands. 22 | $(VERBOSE).SILENT: 23 | 24 | 25 | # A target that is always out of date. 26 | cmake_force: 27 | 28 | .PHONY : cmake_force 29 | 30 | #============================================================================= 31 | # Set environment variables for the build. 32 | 33 | # The shell in which to execute make rules. 34 | SHELL = /bin/sh 35 | 36 | # The CMake executable. 37 | CMAKE_COMMAND = /home/cmp/Downloads/clion-2019.2/bin/cmake/linux/bin/cmake 38 | 39 | # The command to remove a file. 40 | RM = /home/cmp/Downloads/clion-2019.2/bin/cmake/linux/bin/cmake -E remove -f 41 | 42 | # Escaping for special characters. 43 | EQUALS = = 44 | 45 | # The top-level source directory on which CMake was run. 46 | CMAKE_SOURCE_DIR = /home/cmp/work_dir/source_code/yacc_bison_practice 47 | 48 | # The top-level build directory on which CMake was run. 49 | CMAKE_BINARY_DIR = /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug 50 | 51 | # Include any dependencies generated for this target. 52 | include ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/depend.make 53 | 54 | # Include the progress variables for this target. 55 | include ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/progress.make 56 | 57 | # Include the compile flags for this target's objects. 58 | include ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/flags.make 59 | 60 | ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/1.02_BritishEn_to_AmericanEn.c.o: ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/flags.make 61 | ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/1.02_BritishEn_to_AmericanEn.c.o: ch1/1.02_BritishEn_to_AmericanEn.c 62 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --progress-dir=/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/CMakeFiles --progress-num=$(CMAKE_PROGRESS_1) "Building C object ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/1.02_BritishEn_to_AmericanEn.c.o" 63 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1 && /usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -o CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/1.02_BritishEn_to_AmericanEn.c.o -c /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/1.02_BritishEn_to_AmericanEn.c 64 | 65 | ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/1.02_BritishEn_to_AmericanEn.c.i: cmake_force 66 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Preprocessing C source to CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/1.02_BritishEn_to_AmericanEn.c.i" 67 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1 && /usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -E /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/1.02_BritishEn_to_AmericanEn.c > CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/1.02_BritishEn_to_AmericanEn.c.i 68 | 69 | ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/1.02_BritishEn_to_AmericanEn.c.s: cmake_force 70 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green "Compiling C source to assembly CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/1.02_BritishEn_to_AmericanEn.c.s" 71 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1 && /usr/bin/cc $(C_DEFINES) $(C_INCLUDES) $(C_FLAGS) -S /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/1.02_BritishEn_to_AmericanEn.c -o CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/1.02_BritishEn_to_AmericanEn.c.s 72 | 73 | # Object files for target 1.02_BritishEn_to_AmericanEn 74 | 1_02_BritishEn_to_AmericanEn_OBJECTS = \ 75 | "CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/1.02_BritishEn_to_AmericanEn.c.o" 76 | 77 | # External object files for target 1.02_BritishEn_to_AmericanEn 78 | 1_02_BritishEn_to_AmericanEn_EXTERNAL_OBJECTS = 79 | 80 | ch1/1.02_BritishEn_to_AmericanEn: ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/1.02_BritishEn_to_AmericanEn.c.o 81 | ch1/1.02_BritishEn_to_AmericanEn: ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/build.make 82 | ch1/1.02_BritishEn_to_AmericanEn: ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/link.txt 83 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --green --bold --progress-dir=/home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/CMakeFiles --progress-num=$(CMAKE_PROGRESS_2) "Linking C executable 1.02_BritishEn_to_AmericanEn" 84 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1 && $(CMAKE_COMMAND) -E cmake_link_script CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/link.txt --verbose=$(VERBOSE) 85 | 86 | # Rule to build all files generated by this target. 87 | ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/build: ch1/1.02_BritishEn_to_AmericanEn 88 | 89 | .PHONY : ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/build 90 | 91 | ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/clean: 92 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1 && $(CMAKE_COMMAND) -P CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/cmake_clean.cmake 93 | .PHONY : ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/clean 94 | 95 | ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/depend: 96 | cd /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug && $(CMAKE_COMMAND) -E cmake_depends "Unix Makefiles" /home/cmp/work_dir/source_code/yacc_bison_practice /home/cmp/work_dir/source_code/yacc_bison_practice/ch1 /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1 /home/cmp/work_dir/source_code/yacc_bison_practice/cmake-build-debug/ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/DependInfo.cmake --color=$(COLOR) 97 | .PHONY : ch1/CMakeFiles/1.02_BritishEn_to_AmericanEn.dir/depend 98 | 99 | --------------------------------------------------------------------------------