Flex not parsing variable names properly - flex-lexer

I'm new to using Flex. Here's my lex file:
%{
#include <stdio.h>
%}
%%
\w[\w\d]+ printf("WORD\n");
. printf("OTHER\n");
%%
int main() {
yylex();
return 0;
}
I then compile this with
flex lexfile.l && gcc lex.yy.c -ll
However, after running
echo "hello" | ./a.out
I get
UNKNOWN
UNKNOWN
UNKNOWN
UNKNOWN
UNKNOWN
Why wouldn't I get
WORD
?

Ah. Looks like Flex's version of regex doesn't understand \w and \d. Replaced with
[A-Za-z][A-Za-z0-9]+
and it worked fine.

Related

PANIC: unprotected error in call to Lua API (undefined symbol: lua_gettop)

My environment is Lua-5.4.2 Luasocket-3.0-rc1.
When I run lua script directly, it work success.
When i run it through c language, it tell me error.
Error Msg is :
PANIC: unprotected error in call to Lua API (error running script: error loading module 'socket.core' from file '/usr/local/lib/lua/5.4/socket/core.so': undefined symbol: lua_gettop)
Aborted(core dumped)
Does anyone know why?
lua script code is:(test.lua)
#!/usr/local/bin/lua
local socket = require("socket")
print(socket._VERSION)
c code is:(main.c)
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
int main(void)
{
lua_State *L;
L = luaL_newstate();
luaopen_base(L);
luaL_openlibs(L);
printf("lua enter\n");
if (luaL_dofile(L, "test.lua"))
{
luaL_error(L, "error running script: %s", lua_tostring(L, -1));
}
printf("lua exit\n");
while(1) pause();
lua_close(L);
return 0;
}
tl;dr: Pass -Wl,-E to GCC.
I was able to reproduce your problem with this Dockerfile:
FROM gcc:11.1.0
RUN wget https://www.lua.org/ftp/lua-5.4.2.tar.gz \
&& git clone https://github.com/diegonehab/luasocket.git
RUN tar zxf lua-5.4.2.tar.gz \
&& cd lua-5.4.2 \
&& make linux \
&& make install \
&& cd ../luasocket \
&& git checkout 5b18e475f38fcf28429b1cc4b17baee3b9793a62 \
&& make linux LUAV=5.4 \
&& make install LUAV=5.4
COPY test.lua main.c ./
When I run lua test.lua in the resulting Docker container, it works fine. When I run gcc -o test main.c /usr/local/lib/liblua.a -ldl -lm -Wl,-rpath='/usr/local/lib/lua/5.4/socket' && ./test, I get the same panic that you get.
The reason that the standalone Lua binary works and yours doesn't is that the former is linked with -E:
MYLDFLAGS= $(LOCAL) -Wl,-E
ld's documentation for -E says this about it:
If you use dlopen to load a dynamic object which needs to refer back to the symbols defined by the program, rather than some other dynamic object, then you will probably need to use this option when linking the program itself.
Lua uses dlopen to load C modules that you require, and said modules need to call Lua functions, which are linked into your binary, so it makes sense that you need this option. And indeed, when I add -Wl,-E to the GCC command line, then it works fine for me:
root#077bbb831441:/# gcc -o test main.c /usr/local/lib/liblua.a -ldl -lm -Wl,-rpath='/usr/local/lib/lua/5.4/socket' -Wl,-E && ./test
lua enter
LuaSocket 3.0-rc1
lua exit

pcap "undefined reference to" error

I'm trying to read in data from pcap files using pcap_open_offline(). I've used #include <pcap/pcap.h> and compiled with no errors after some debugging. Now I've come across another problem I can't seem to figure out. I wrote the following function:
void openPcap(char* filename){
printf("Opening file %s\n", filename);
pcap_t *pcap;
const unsigned char *packet;
char errbuf[PCAP_ERRBUF_SIZE];
struct pcap_pkthdr header;
pcap = pcap_open_offline(filename, errbuf);
if (pcap == NULL){
fprintf(stderr, "%s Malformed packet records in file %s",ER,filename);
}
}
And my pcap_open_offline call gives me about 100 of these errors when I try to compile:
pcap-linux.c:(.text+0xcd4): undefined reference to 'nl_handle_alloc'
pcap-linux.c:(.text+0xce8): undefined reference to 'genl_connect'
pcap-linux.c:(.text+0xcf6): undefined reference to 'genl_ctrl_alloc_cache'
pcap-linux.c:(.text+0xd0e): undefined reference to 'genl_ctrl_search_by_name'
pcap-linux.c:(.text+0xd64): undefined reference to 'nl_handle_destroy'
pcap-linux.c:(.text+0xdd7): undefined reference to 'nl_cache_free'
This is what my makefile looks like:
# -------------------------------
C=/afs/nd.edu/user14/csesoft/new/bin/gcc
CFLAGS=-Wall -std=c11 -I/afs/nd.edu/coursesp.18/cse/cse30341.01/support/gcc-libpcap/include -D_BSD_SOURCE
LD=/afs/nd.edu/user14/csesoft/new/bin/g++
#LD=g++
LDFLAGS=-lpthread
# # ----------------------------
LDFLAGS += -L/afs/nd.edu/coursesp.18/cse/cse30341.01/support/gcc-libpcap/lib -lpcap # Add your own flags here, or leave blank
threadedRE: threadedRE.o
$(LD) $^ $(LDFLAGS) -o $#
threadedRE.o: threadedRE.c
$(C) $(CFLAGS) -c $<
# C compiler
%.o: %.c
$(C) $(CFLAGS) -c $<
.PHONY: clean
clean:
rm -f threadedRE *.o
And my headers are:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <sys/stat.h>
#include <pcap/pcap.h>
Any suggestions would be greatly appreciated.
It isn't a compiling error, but a linking one. At the end of build process you should see something like "ld exited with error".
pcap_open_offline() seems to use nl_handle_alloc() and other functions, but linker can't find object files containing their implementation. Pointing linker to proper library which contains required object files by adding -lnl to LDFLAGS should do the trick.

Using Opencv 3.1 from CAFFE

I am trying to use OPENCV 3.1 from inside of the caffe . This is my test code
#include <caffe/caffe.hpp>
#ifdef USE_OPENCV
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/utility.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#endif // USE_OPENCV
#include<iostream>
#ifdef USE_OPENCV
int main(){
std::cout<<"USE_OPENCV value is 1\n";
return 0;
}
#else
int main(){
std::cout<<"USE_OPENCV value is 0\n";
return 0;
}
#endif
I am compiling it using following command
g++ -I path_to_caffe/distribute/include/ test3.cpp -std=c++0x -lboost_system
It compiles with giving the following output while executing :
USE_OPENCV value is 0
Before compiling the caffe i make this changes in Makefile.config file:
USE_OPENCV := 1
. . .
OPENCV_VERSION := 3
. . .
USE_PKG_CONFIG := 1
While compiling caffe, I firstly cleand it and then compiled it using following commands:
make clean
make all -j $(($(nproc) + 1))
make test
make runtest
make pycaffe
make distribute
It compiles without giving error , but while testing my test file it does not gives the output that i expect.
All glories goes to Shai. The solution is to add -DUSE_OPENCV flag .
g++ -I path_to_caffe/distribute/include/ test3.cpp -std=c++0x -lboost_system -DUSE_OPENCV

LLVM/Clang use opt to show linked ll file

I had two c files and want show IR for its linked bit code
link1.c
#include "link2.h"
int main() {
int a;
int b;
foo(a,b);
return 0;
}
link2.h
#include<stdio.h>
link2.c
#include "link2.h"
void foo(int a, int b) {
printf("%d\n", a);
printf("%d\n", b);
}
I did the following command to get bc file.
clang -o0 -emit-llvm file1.c -c -o file1.bc
clang -o0 -emit-llvm file2.c -c -o file2.bc
llvm-link -o link.bc link1.bc link2.bc
When I tried lli link.bc and llvm-dis link.bc it run correctly and showed linked ll file, but when I use opt link.bc -S -o link.ll to get ll file it just reported segmentation error. Can anyone let me know what to do with opt?
It seems like it is because version compatible issue of opt. I should use a 3.5 version but turns out I was using 3.4.2.

How to use Intel AVX on QNX Neutrino 6.5.0?

I recently started working with QNX 6.5.0 and can't understand how in QNX develop programs using Intel AVX. Installed QNX Development Studio 6.5.0 with GCC 4.4.2, I'm trying to write a simple program, but the build fails.
#include <immitnrin.h>
int main( int argc, char** argv )
{
__m256 var;
__m256 var2;
__m256 result;
var = _mm256_set1_ps(1.f);
var2 = _mm256_set1_ps(3.f);
result = _mm256_add_ps(var,var2);
return 0;
}
error: immitnrin.h: No such file or directory
In function 'int main(int, char**)':
error: '__m256' was not declared in this scope
error: expected ';' before 'var'
error: expected ';' before 'var2'
error: expected ';' before 'result'
error: 'var' was not declared in this scope
error: '_mm256_set1_ps' was not declared in this scope
error: 'var2' was not declared in this scope
error: 'result' was not declared in this scope
error: '_mm256_add_ps' was not declared in this scope
How and where can I learn how to work with SIMD instructions Intel in QNX?
Update
Here's the output from the program make.exe:
make.exe -k CPULIST=x86 all --file=C:/DOCUME~1/Andrew/LOCALS~1/Temp/QMakefile.tmp
C:/QNX650/host/win32/x86/usr/bin/make.exe -j 1 -Cx86 -fMakefile all
make.exe[1]: Entering directory C:/ide-4.7-workspace/project_test_avx/x86'
C:/QNX650/host/win32/x86/usr/bin/make.exe -j 1 -Co -fMakefile all
make.exe[2]: Entering directoryC:/ide-4.7-workspace/project_test_avx/x86/o'

Resources